【问题标题】:How to remove blank line created in the XML file after deleting a particular atrribute删除特定属性后如何删除在 XML 文件中创建的空白行
【发布时间】:2012-04-10 03:35:48
【问题描述】:

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

【问题讨论】:

  • 这可能就是您打印出来的方式。实际文档中可能不存在空行。您如何尝试解析/打印 XML?另外,我认为您对 XML 属性与 XML 元素的概念有点模糊。 &lt;attribute id="3"/&gt; 实际上是一个 XML 元素。

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


【解决方案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.
    }
}

【讨论】:

  • 如何将我的文档转换为 Document 类型?
猜你喜欢
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-24
  • 2016-12-26
  • 1970-01-01
  • 2013-08-25
相关资源
最近更新 更多