0

たとえば、XMLドキュメントがあります

<document>
    <attribute id = 1 />
    <attribute id = 2 />
    <attribute id = 3 />
    <attribute id = 4 />
    <attribute id = 5 />
    <attribute id = 6 />
</document>

C++コードで DOM パーサーを使用して XML ファイルを解析しています。id = 3 などの特定の属性を削除しています。

Xerces ライブラリの API を使用して属性を削除しましたが、属性を削除した場所に空白行が表示されます。

削除は次のように行います。指定されたファイルから必要な属性を削除し、残りの内容を一時ファイルにコピーしますが、空白行が作成されます。

<document>
    <attribute id = 1 />
    <attribute id = 2 />

    <attribute id = 4 />
    <attribute id = 5 />
    <attribute id = 6 />
</document>

次のような出力が必要です。削除後にファイルに空白行が存在しないようにする必要があります

<document>
    <attribute id = 1 />
    <attribute id = 2 />
    <attribute id = 4 />
    <attribute id = 5 />
    <attribute id = 6 />
</document>
4

1 に答える 1

1

この方法を使用して、目的を達成します...

private static void formatWSDL(Document doc, String path) {
    try {
        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(path)), format);
        serializer.serialize(doc.getDocumentElement());
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
}
于 2012-06-03T10:36:04.703 に答える