• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating formatted XML output by JAXP

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Create symphonies in seed and soil. For this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic