Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StAX XML formatting in Java

Tags:

Is it possible using StAX (specifically woodstox) to format the output xml with newlines and tabs, i.e. in the form:

 <element1>   <element2>    someData   </element2> </element1> 

instead of:

<element1><element2>someData</element2></element1>

If this is not possible in woodstox, is there any other lightweight libs that can do this?

like image 616
Lehane Avatar asked Nov 14 '08 14:11

Lehane


People also ask

How to write a XML file in Java?

Steps to create and write XML to a file. Create a Document doc . Create XML elements, attributes, etc., and append to the Document doc . Create a Transformer to write the Document doc to an OutputStream .

How read and write data from XML in Java?

We must have followed the process to read an XML file in Java: Instantiate XML file: DOM parser loads the XML file into memory and consider every tag as an element. Get root node: Document class provides the getDocumentElement() method to get the root node and the element of the XML file.


1 Answers

There is com.sun.xml.txw2.output.IndentingXMLStreamWriter

XMLOutputFactory xmlof = XMLOutputFactory.newInstance(); XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(out)); 
like image 161
Juha Syrjälä Avatar answered Oct 16 '22 07:10

Juha Syrjälä