StAX XML formatting in Java
Asked Answered
Q

10

26

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?

Quijano answered 14/11, 2008 at 14:59 Comment(0)
E
7

Via the JDK: transformer.setOutputProperty(OutputKeys.INDENT, "yes");.

Epicanthus answered 14/11, 2008 at 15:50 Comment(6)
The link to the approach misses a colon after the httpsLabour
@Epicanthus The link is broken. This approach is proposed on SO : Formatting XML file using StAXStationary
I added a bit more context in my answer: https://mcmap.net/q/109900/-stax-xml-formatting-in-javaNeedlecraft
That is not answer. What transformer? How to use it?Sharanshard
@pavel_k docs.oracle.com/javase/7/docs/api/index.html?javax/xml/… I would assume that if you are programming in Java you know how to reference Java documentation and have attempted to first implement the solution yourselfEpicanthus
see also the OutputKeys class in the same package docs.oracle.com/javase/7/docs/api/index.html?javax/xml/…Epicanthus
U
25

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

XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(out));
Untrue answered 2/9, 2010 at 8:29 Comment(2)
as far as I can see the namespace either has an error in it or has moved to com.sun.xml.internal.txw2.output.Nide
@Nide No, the "internal" version is bundled with the JRE. You can get the non-internal version by explicitly adding JAXB-RI as a dependency.Bundle
G
8

If you're using the StAX cursor API, you can indent the output by wrapping the XMLStreamWriter in an indenting proxy. I tried this in my own project and it worked nicely.

Gilmore answered 3/3, 2010 at 4:9 Comment(0)
N
8

Using the JDK Transformer:

public String transform(String xml) throws XMLStreamException, TransformerException
{
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    Writer out = new StringWriter();
    t.transform(new StreamSource(new StringReader(xml)), new StreamResult(out));
    return out.toString();
}
Needlecraft answered 14/7, 2016 at 10:34 Comment(0)
E
7

Via the JDK: transformer.setOutputProperty(OutputKeys.INDENT, "yes");.

Epicanthus answered 14/11, 2008 at 15:50 Comment(6)
The link to the approach misses a colon after the httpsLabour
@Epicanthus The link is broken. This approach is proposed on SO : Formatting XML file using StAXStationary
I added a bit more context in my answer: https://mcmap.net/q/109900/-stax-xml-formatting-in-javaNeedlecraft
That is not answer. What transformer? How to use it?Sharanshard
@pavel_k docs.oracle.com/javase/7/docs/api/index.html?javax/xml/… I would assume that if you are programming in Java you know how to reference Java documentation and have attempted to first implement the solution yourselfEpicanthus
see also the OutputKeys class in the same package docs.oracle.com/javase/7/docs/api/index.html?javax/xml/…Epicanthus
T
4

How about StaxMate:

http://www.cowtowncoder.com/blog/archives/2006/09/entry_21.html

Works well with Woodstox, fast, low-memory usage (no in-memory tree built), and indents like so:


SMOutputFactory sf = new SMOutputFactory(XMLOutputFactory.newInstance());
SMOutputDocument doc = sf.createOutputDocument(new FileOutputStream("output.xml"));
doc.setIndentation("\n ", 1, 2); // for unix linefeed, 2 spaces per level    
// write doc like:    
SMOutputElement root = doc.addElement("element1");    
root.addElement("element2").addCharacters("someData");    
doc.closeRoot(); // important, flushes, closes output

Tezel answered 27/1, 2009 at 20:40 Comment(0)
P
4

Rather than relying on a com.sun...class that might go away (or get renamed com.oracle...class), I recommend downloading the StAX utility classes from java.net. This package contains a IndentingXMLStreamWriter class that works nicely. (Source and javadoc are included in the download.)

Pieper answered 14/2, 2013 at 15:32 Comment(0)
I
2

if you are using XMLEventWriter, then an easier way to do that is:

XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter writer = outputFactory.createXMLEventWriter(w);
        XMLEventFactory eventFactory = XMLEventFactory.newInstance();
        Characters newLine = eventFactory.createCharacters("\n"); 
        writer.add(startRoot);
        writer.add(newLine);
Itagaki answered 1/7, 2018 at 12:16 Comment(0)
C
1

If you're using the iterating method (XMLEventReader), can't you just attach a new line '\n' character to the relevant XMLEvents when writing to your XML file?

Cryostat answered 14/11, 2008 at 15:49 Comment(0)
G
1

Not sure about stax, but there was a recent discussion about pretty printing xml here

pretty print xml from java

this was my attempt at a solution

How to pretty print XML from Java?

using the org.dom4j.io.OutputFormat.createPrettyPrint() method

Guardi answered 24/11, 2008 at 23:51 Comment(0)
D
0

With Spring Batch this requires a subclass since this JIRA BATCH-1867

public class IndentingStaxEventItemWriter<T> extends StaxEventItemWriter<T> {

  @Setter
  @Getter
  private boolean indenting = true;

  @Override
  protected XMLEventWriter createXmlEventWriter( XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException {
    if ( isIndenting() ) {
      return new IndentingXMLEventWriter( super.createXmlEventWriter( outputFactory, writer ) );
    }
    else {
      return super.createXmlEventWriter( outputFactory, writer );
    }
  }

}

But this requires an additionnal dependency because Spring Batch does not include the code to indent the StAX output:

<dependency>
  <groupId>net.java.dev.stax-utils</groupId>
  <artifactId>stax-utils</artifactId>
  <version>20070216</version>
</dependency>
Disenchant answered 18/9, 2013 at 15:20 Comment(1)
I tried this, but when i deploy my application on weblogic and run the job it creates an xml file with &#xd; in each line after the closing tag. How to avoid this?Stephan

© 2022 - 2024 — McMap. All rights reserved.