如何删除特定属性后删除XML文件中创建的空行

时间:2012-03-26 15:53:02

标签: c++ xml xml-parsing xerces domparser

我有一个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>

1 个答案:

答案 0 :(得分: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.
    }
}