12

我正在使用 StAX 创建一个相当大的 xml 文档。到目前为止,我一直在使用 IndentingXMLStreamwriter 类来获取格式良好的文档(另请参阅此答案)。几天前,我们使用较旧的 jdk 版本(6.26)设置了 jenkins 服务器,在该服务器上出现构建错误。

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

我假设由于安装了 jdk 版本而无法找到该软件包。由于不同的原因,这无法更改(顺便说一句,有没有人知道添加了这个包(com.sun.xml.internal.txw2.output)的jdk版本?)。
因此,我正在寻找一种替代方法来进行缩进。我更喜欢与我使用的解决方案类似的解决方案,这意味着无需重新解析文档。有什么想法或建议吗?

谢谢拉斯
_

4

5 回答 5

14

而不是可以在以下位置找到的com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter用途:com.sun.xml.txw2.output.IndentingXMLStreamWriter

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>txw2</artifactId>
    <version>2.2.11</version>
</dependency>
于 2015-11-25T12:59:22.997 回答
9

只是扩展迈克尔凯的答案(https://stackoverflow.com/a/10108591/2722227)。

maven依赖:

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

爪哇代码:

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

}
于 2015-06-04T07:22:06.693 回答
4

如果其他建议不起作用,您可以从 Saxon 获取缩进 XMLStreamWriter,如下所示:

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

一个优点是,这允许您使用其他序列化属性对序列化进行大量控制。

于 2012-04-11T14:59:33.123 回答
4

有一个 IndentingXmlStreamWriter 的替代实现,它作为开源 stax-utils 项目的一部分在这里提供:http: //java.net/projects/stax-utils/pages/Home

stax-utils 似乎是一个项目,旨在为 Java 提供基于 jsr-173 流 xml api 的实用程序

您需要将 stax-utils jar 添加为项目的依赖项。然后你可以导入 javanet.staxutils.IndentingXmlStreamWriter

由于 stax-utils 在 maven 中央存储库中,如果您使用 maven 作为依赖项,您可以通过以下方式获得它:

    <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>

功能似乎与 txw2 类非常相似/等效

因为我使用的是 jdk 1.7,所以我已经排除了 jsr173-ri。我认为 1.6+ 将 jsr173 api 作为标准功能,但如果您使用 1.5 或更低版本,则需要额外的 jsr173 jar。

于 2013-01-07T23:35:57.927 回答
1

如果您使用的是 Maven 和 Java 8,则可以导入以下内容以使用此类:

        <!-- 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>

然后,将其导入为:import com.sun.xml.txw2.output.IndentingXMLStreamWriter;

于 2019-10-15T21:34:59.763 回答