0

例如,我有一个 XML 文档

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

我正在使用 DOM 解析器来解析C++代码中的 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 回答