GeeCON Prague 2014*
The moose likes XML and Related Technologies and the fly likes Creating formatted XML output by JAXP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Creating formatted XML output by JAXP" Watch "Creating formatted XML output by JAXP" New topic
Author

Creating formatted XML output by JAXP

Gourab Guha
Greenhorn

Joined: Nov 06, 2006
Posts: 13
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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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.
 
GeeCON Prague 2014
 
subject: Creating formatted XML output by JAXP