• 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

New elements of DOM structure on new lines ??

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using the code below to output a regular DOM structure to an XML file. This code was picked up from the JAVA site and used with very little modification. The problem that I face is basically the following.
Unlike the tutorials which I am following along, the elements that I print to the file are not getting printed onto new lines but instead all of the DOM structure gets printed on a single line. Is there a quick fix around this problem.

Thanks in advance


public static void writeXmlToFile(String filename, Document document) {
try {
// Prepare the DOM document for writing
Source source = new DOMSource(document);

// Prepare the output file
File file = new File(filename);
Result result = new StreamResult(file);


// Write the DOM document to the file
// Get Transformer
Transformer xformer = TransformerFactory.newInstance().newTransformer();
// Write to a file
xformer.transform(source, result);
xformer.setOutputProperty( "indent", "yes" );
xformer.setOutputProperty("method","xml");


} catch (TransformerConfigurationException e) {
System.out.println("TransformerConfigurationException: " + e);
} catch (TransformerException e) {
System.out.println("TransformerException: " + e);
}
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code you posted is the only "standard" way of trying to affect the indentation of the printed XML. If you're ok with using parser-specific APIs for this, and if you're using Xerces, take a look at HowToPrettyPrintXmlWithJava in our wiki FAQ.
 
reply
    Bookmark Topic Watch Topic
  • New Topic