Search code examples
javaxmlindentationstax

Java: IndentingXMLStreamWriter alternative?


I am using StAX to create a quite large xml document. Until now I was using the IndentingXMLStreamwriter class to get a well formatted document (see also this answer). A few days ago we setup a jenkins server with an older jdk version (6.26), on which i get build errors.

package com.sun.xml.internal.txw2.output does not exist

I assume the package cannot be found because of the installed jdk version. For different reasons this cannot be changed (by the way, does anyone know the jdk version, at which this package (com.sun.xml.internal.txw2.output) was added?).
Therefore I am looking for an alternative to do the indenting. I would prefer a solution similar to the one I was using, which means without reparsing the document. Any ideas or suggestions?

Thanks
Lars


Solution

  • If other suggestions don't work, you can get an indenting XMLStreamWriter from Saxon like this:

    Processor p = new net.sf.saxon.s9api.Processor();
    Serializer s = p.newSerializer();
    s.setOutputProperty(Property.METHOD, "xml");
    s.setOutputProperty(Property.INDENT, "yes");
    s.setOutputStream(....);
    XMLStreamWriter writer = s.getXMLStreamWriter();
    

    One advantage is that this allows you a lot of control over the serialization using other serialization properties.