Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 944
Lars Avatar asked Apr 11 '12 11:04

Lars


2 Answers

Just expanding on Michael Kay's answer ( https://stackoverflow.com/a/10108591/2722227 ).

maven dependencies:

<dependency>
            <groupId>net.sf.saxon</groupId>
            <artifactId>Saxon-HE</artifactId>
            <version>9.6.0-5</version>
</dependency>

java code:

import java.io.OutputStream;
import net.sf.saxon.Configuration;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.Serializer.Property;

public class Main {

    public static void main(String[] args) throws Exception {
        OutputStream outputStream = System.out;
        writeXmlDocument(outputStream);
    }

    private static void writeXmlDocument(OutputStream outputStream){
        Configuration config = new Configuration();
        Processor processor = new Processor(config);
        Serializer serializer = processor.newSerializer();
        serializer.setOutputProperty(Property.METHOD, "xml");
        serializer.setOutputProperty(Property.INDENT, "yes");
        serializer.setOutputStream(outputStream);

        try {
            XMLStreamWriter writer = serializer.getXMLStreamWriter();
            try {
                writer.writeStartDocument();
                {
                    writer.writeStartElement("root_element_name");
                    {
                        writer.writeStartElement("child_element");
                        writer.writeEndElement();
                    }
                    writer.writeEndElement();
                }
                writer.writeEndDocument();
                writer.flush();
                writer.close();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }

        } catch (SaxonApiException e) {
            e.printStackTrace();
        }
    }

}
like image 66
Robert Fey Avatar answered Oct 06 '22 18:10

Robert Fey


Instead of com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter use com.sun.xml.txw2.output.IndentingXMLStreamWriter that can be found in:

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>txw2</artifactId>
    <version>2.2.11</version>
</dependency>
like image 39
qnox Avatar answered Oct 06 '22 19:10

qnox