Java: IndentingXMLStreamWriter alternative?
Asked Answered
D

5

12

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

Denishadenison answered 11/4, 2012 at 11:41 Comment(1)
This answer (https://mcmap.net/q/111658/-formatting-xml-file-using-stax) seems to deal with the same problem.Hinson
F
4

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.

Finely answered 11/4, 2012 at 14:59 Comment(1)
I've added a more complete example below: https://mcmap.net/q/392523/-java-indentingxmlstreamwriter-alternativeRegardless
B
15

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>
Bochum answered 25/11, 2015 at 12:59 Comment(1)
We had a similar issue and this was the best way to solve it, a more up-to-date library compared stax-utils, thanks, voted up.Manoeuvre
R
9

Just expanding on Michael Kay's answer ( https://mcmap.net/q/392523/-java-indentingxmlstreamwriter-alternative ).

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();
        }
    }

}
Regardless answered 4/6, 2015 at 7:22 Comment(0)
F
4

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.

Finely answered 11/4, 2012 at 14:59 Comment(1)
I've added a more complete example below: https://mcmap.net/q/392523/-java-indentingxmlstreamwriter-alternativeRegardless
K
4

There is an alternative implementation of IndentingXmlStreamWriter, which is provided as part of the open source stax-utils project here: http://java.net/projects/stax-utils/pages/Home

stax-utils seems to be a project set up to provide utilities based around the jsr-173 streaming xml api for Java

You'd need to add the stax-utils jar as a dependency for your project. Then you can import javanet.staxutils.IndentingXmlStreamWriter

Since stax-utils is in the maven central repository, if you use maven for your dependencies you can get it with:

    <dependency>
        <groupId>net.java.dev.stax-utils</groupId>  
        <artifactId>stax-utils</artifactId>
        <version>20070216</version>
        <exclusions>
            <exclusion>
                <groupId>com.bea.xml</groupId>
                <artifactId>jsr173-ri</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Functionality seems very similar / equivalent to the txw2 class

I have excluded jsr173-ri since I am using jdk 1.7. I think 1.6+ has the jsr173 api as a standard feature, but if you are using 1.5 or lower you'd need the extra jsr173 jar.

Kentigerma answered 7/1, 2013 at 23:35 Comment(0)
T
1

If you are using Maven and Java 8, you can import the following to use this class:

        <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.1.17</version>
        </dependency>

And then, you import it as: import com.sun.xml.txw2.output.IndentingXMLStreamWriter;

Telescope answered 15/10, 2019 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.