Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:缩进XmlStreamWriter替代方案?_Java_Xml_Indentation_Stax - Fatal编程技术网

Java:缩进XmlStreamWriter替代方案?

Java:缩进XmlStreamWriter替代方案?,java,xml,indentation,stax,Java,Xml,Indentation,Stax,我正在使用StAX创建一个相当大的xml文档。到目前为止,我一直在使用IndentingXMLStreamwriter类来获取格式良好的文档(另请参见)。几天前,我们使用较旧的jdk版本(6.26)安装了一台jenkins服务器,在该版本上我会遇到构建错误 package com.sun.xml.internal.txw2.output does not exist 我假设由于安装了jdk版本,无法找到该包。由于不同的原因,这一点无法改变 (顺便问一下,有人知道添加此包(com.sun.xml

我正在使用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版本吗?。
因此,我正在寻找一个替代做缩进。我更喜欢一个与我正在使用的解决方案类似的解决方案,这意味着不需要重新分析文档。有什么想法或建议吗

谢谢

Lars

如果其他建议无效,您可以从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();

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

这里有一个IndentingXmlStreamWriter的替代实现,它是作为开放源代码stax utils项目的一部分提供的:

stax utils似乎是一个基于jsr-173 Java流式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>

net.java.dev.stax-utils
斯塔克斯乌提尔斯酒店
20070216
com.bea.xml
jsr173 ri
功能似乎与txw2类非常相似/等效


自从我使用JDK1.7以来,我已经排除了jsr173 ri。我认为1.6+将jsr173 api作为标准特性,但如果您使用1.5或更低版本,则需要额外的jsr173 jar。

仅扩展Michael Kay的答案()

maven依赖项:

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

使用可在以下位置找到的
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>

org.glassfish.jaxb
txw2
2.2.11

如果您使用的是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>

com.sun.xml.bind
jaxb impl
2.1.17

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

这个答案()似乎解决了同样的问题。我在下面添加了一个更完整的示例:我们有一个类似的问题,这是解决它的最佳方法,与stax UTIL相比,它是一个更新的库,谢谢,投票支持。
        <!-- 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>