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

Michael Kayの答えを拡張するだけです(https://stackoverflow.com/a/10108591/2722227)。

Mavenの依存関係:

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

Javaコード:

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

1つの利点は、これにより、他のシリアル化プロパティを使用してシリアル化を大幅に制御できることです。

于 2012-04-11T14:59:33.123 に答える
4

IndentingXmlStreamWriterの代替実装があります。これは、オープンソースのstax-utilsプロジェクトの一部としてここに提供されています:http://java.net/projects/stax-utils/pages/Home

stax-utilsは、Java用のjsr-173ストリーミングxmlapiに基づいたユーティリティを提供するために設定されたプロジェクトのようです。

プロジェクトの依存関係としてstax-utilsjarを追加する必要があります。次に、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以降にはjsr173apiが標準機能として含まれていると思いますが、1.5以下を使用している場合は、追加のjsr173jarが必要になります。

于 2013-01-07T23:35:57.927 に答える
1

MavenとJava8を使用している場合は、以下をインポートしてこのクラスを使用できます。

        <!-- 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 に答える