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

标签 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>

最佳答案

使用这个方法可以达到想要的...

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.
    }
}

关于c++ - 删除特定属性后如何删除在 XML 文件中创建的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9875400/

相关文章:

java - 注释节点 JaxB 编码

c++ - 这两个程序有什么区别

c++ - va_end 到底是做什么用的?是否总是需要调用它?

xml - 实体 "HTML.Version"的声明必须以 '>' 结尾

mysql - 读取大转储文件

ios - 在 iOS 上解析 XML

c# - C++ 和 C# 中的等效系统时钟毫秒数?

C++ 转换会创建一个新对象吗?

java - Javadoc 是否具有 &lt;![CDATA[ ... ]]> 的等价物?

python - 通过 'ElementTree' 在 Python 中使用命名空间解析 XML