Converting DOM to text 2003-01-20 - By Gary L Peskin
See http://xml.apache.org/xalan-j/faq.html#faq-N10237
Change your
transformer.transform(xmlDomSource, domResult);
to
transformer.transform(xmlDomSource, new StreamResult(System.out));
and I think you'll get what you're looking for. You'll also see what's causing the HIERARCHY_REQUEST_ERR.
HTH, Gary
-----Original Message----- From: Sean Leblanc [mailto:SeanL@(protected)] Sent: Monday, January 20, 2003 10:05 AM To: 'xalan-j-users@(protected)' Subject: Converting DOM to text
Hello,
I am trying to use the samples to convert DOM I already have in memory to a string of text.
I'm looking at the DOM2DOM sample, and trying to use it, but I get this error:
javax.xml.transform.TransformerException: org.w3c.dom.DOMException: HIERARCHY_REQUEST_ERR: An attempt was made to insert a node where it is not permitted.
Here is the code. The variable "document" is passed in.
// Get ready for a transformation: DOMSource xmlDomSource = new DOMSource(document); DocumentBuilderFactory dFactory DocumentBuilderFactory.newInstance(); dFactory.setNamespaceAware(true); DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); TransformerFactory tFactory = TransformerFactory.newInstance(); // Get our XSL file, transform into source: File xslFile = new File("test.xsl"); System.out.println("Exists? " + xslFile.exists()); Document xslDoc = dBuilder.parse(xslFile); DOMSource xslDomSource = new DOMSource(xslDoc);
// Set the systemId: note this is actually a URL, not a local filename xslDomSource.setSystemId("");
// Process the stylesheet DOMSource and generate a Transformer. Transformer transformer = tFactory.newTransformer(xslDomSource);
// Create an empty DOMResult for the Result. DOMResult domResult = new DOMResult();
// Perform the transformation, placing the output in the DOMResult. transformer.transform(xmlDomSource, domResult);
//Instantiate an Xalan XML serializer and use it to serialize the output DOM to System.out // using a default output format. Serializer serializer = SerializerFactory.getSerializer
(OutputProperties.getDefaultMethodProperties("xml")); serializer.setOutputStream(System.out); serializer.asDOMSerializer().serialize(domResult.getNode()); This e-mail, including attachments, is intended for the person(s) or company named and may contain confidential and/or legally privileged information. Unauthorized disclosure, copying or use of this information may be unlawful and is prohibited. If you are not the intended recipient, please delete this message and notify the sender.
|
|