例如,我有一个 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/