• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating formatted XML output by JAXP

 
Gourab Guha
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have written a very simple Java program to write a XML document. I am using the default implementation of DOM provided by Sun, that means javax.xml and org.w3c.dom packages.

Here is the code snippet.

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();

// The following property is used to create the element in different lines of the xml
transformer.setOutputProperty(OutputKeys.INDENT,"Yes");

Source input = new DOMSource(document); // passing the instance of Document
Result output = new StreamResult(os); // passing the instance of OutputStream
transformer.transform(input , output);

The output created is like this

<root>
<element att=�val1� >
<subElement>text</subElement>
</element>
</root>

I want the output will be like this

<root>
<element att=�val1� >
<subElement>text</subElement>
</element>
</root>

Please help me how can I create the output like the above. Is it possible to create such transformation by sun default DOM implementation? (which I am using).

Regards,
Gourab
 
Paul Clapham
Sheriff
Pie
Posts: 20725
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They look the same to me.

But you just didn't use the CODE tag that preserves indenting. I can still guess what you meant.

I looked in the API documentation for the OutputKeys.INDENT constant, and it contained a link to the XSLT Recommendation. Here's what that says about the "indent" attribute:
If the indent attribute has the value yes, then the xml output method may output whitespace in addition to the whitespace in the result tree (possibly based on whitespace stripped from either the source document or the stylesheet) in order to indent the result nicely; if the indent attribute has the value no, it should not output any additional whitespace. The default value is no.
You didn't provide either "yes" or "no" for the value, so the default ("no") was used.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic