0

Trying to execute Simple parsing from dom.Document to String using javax.xml.transform.Transformer , it results an empty string.

Code :

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
DOMSource source = new DOMSource(doc);
StreamResult console = new StreamResult(sw);
transformer.transform(source, console);

Debug Results:

System.out.println(source.getNode().getFirstChild().getNodeName());

                     <root element is displayed>

System.out.println("DONE :"+ console.getWriter().toString());

                      <Empty string is displayed>

Please me know why I get empty string , whereas I need to get the transformed xml string

Blockquote

  • A Transformer needs a stylesheet that contains the rules how to transform. You don't set a stylesheet. – Ralf Renz May 23 '18 at 12:42
  • I did not find any example with style sheet. I guess style sheet is required only when we use XSLT. But here I just parse the javax.dom.document into a xml string. – krishna May 23 '18 at 13:00
  • I think your question is not new: look at https://stackoverflow.com/questions/5456680/xml-document-to-string – Ralf Renz May 23 '18 at 13:35
  • In above link the query to print the output string with/without indent. But in our case we do not get output itself. xml string is empty – krishna May 23 '18 at 13:49

0 Answers0