4

I am using StAX XML stream writer to write the XML file. It writes all the data in a single line. I want all the tags to be indented instead of a single line.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Anurag
  • 311
  • 4
  • 8
  • 14
  • Duplicate of http://stackoverflow.com/questions/290326/stax-xml-formatting-in-java – skaffman Jun 01 '10 at 11:12
  • 2
    @skaffman: From that I am not able to understand how to set the indent parmeter and where I can pass my xml file name. – Anurag Jun 01 '10 at 12:20

3 Answers3

9

stax-utils provides class IndentingXMLStreamWriter which does the job:

XMLStreamWriter writer =
  XMLOutputFactory.newInstance().createXMLStreamWriter(...);
writer = new IndentingXMLStreamWriter(writer);
...
Barend
  • 17,296
  • 2
  • 61
  • 80
chris
  • 3,573
  • 22
  • 20
  • can you please name the jar file. I am not getting the download option at the given site. – Anurag Jun 02 '10 at 10:39
  • @Anurag - https://stax-utils.dev.java.net/files/documents/1519/50947/stax-utils-20070216.zip – chris Jun 03 '10 at 12:21
  • @Chris i tried this with spring batch. I get the xml in proper format, but when i deploy this to weblogic and run the batch job, i see " " inserted at the end of each line. How to avoid this? – Maverick Riz Apr 23 '15 at 21:47
  • @Maverick: Not sure... You may want to try [StAXON](https://github.com/beckchr/staxon) instead, which has a similar class (`de.odysseus.staxon.xml.util.PrettyXMLStreamWriter `). – chris Apr 30 '15 at 10:00
4

Answered here: StAX XML formatting in Java

EDIT: A quick example (without resource cleaning) using stax-utils (https://stax-utils.dev.java.net/):

XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
FileOutputStream file = new FileOutputStream("d:/file.xml");
XMLEventWriter writer = xmlOutputFactory.createXMLEventWriter(file);
writer = new IndentingXMLEventWriter(writer);
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
writer.add(eventFactory.createStartDocument());
writer.add(eventFactory.createStartElement("", "", "a"));
writer.add(eventFactory.createStartElement("", "", "b"));
writer.add(eventFactory.createEndElement("", "", "b"));
writer.add(eventFactory.createEndElement("", "", "a"));
writer.add(eventFactory.createEndDocument());

This gives you:

<?xml version="1.0" encoding="UTF-8"?>
<a>
  <b></b>
</a>
Community
  • 1
  • 1
k_b
  • 2,470
  • 18
  • 9
  • I did check that, can you give some example having the code snippet to pass my xml file and set the indent option. In the example provided at the above location, I am not able to understand how to set all this params. – Anurag Jun 01 '10 at 12:00
  • I added an example to the post. – k_b Jun 01 '10 at 12:49
1

Example pretty printing OMElement (Axiom library) via StAX:

OMElement mapArg = fac.createOMElement(name, elementNs);
mapArg.addAttribute("type", soapXml.getPrefix() + ":Map", xsi);
PropertyDescriptor[] properties = PropertyUtils.getPropertyDescriptors(value);
for (PropertyDescriptor property : properties) {
    if (property.getName().equals("class"))
        continue;
    try {
        mapArg.addChild(keyValue(property.getName(),
                PropertyUtils.getProperty(value, property.getName())));
    } catch (Exception e) {
    }
}
final StringWriter stringWriter = new StringWriter();
try {
    IndentingXMLStreamWriter xmlWriter = new IndentingXMLStreamWriter(StaxUtilsXMLOutputFactory.newInstance().createXMLStreamWriter(stringWriter));
    mapArg.serialize(xmlWriter);
    System.out.println(stringWriter.toString());
} catch (XMLStreamException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Rudi Wijaya
  • 872
  • 1
  • 10
  • 25