File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Creating formatted XML output by JAXP

Gourab Guha

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

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

<element att=�val1� >

I want the output will be like this

<element att=�val1� >

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).

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

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.
I agree. Here's the link:
subject: Creating formatted XML output by JAXP
It's not a secret anymore!