2012-03-26 82 views
0

我有一个XML文档后如何删除XML文件中创建空行,例如删除特定atrribute

<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

使用API​​的从Xerces的库中,我删除属性, 但我得到在我删除了 属性的地方一个空行。

删除如下进行。我将从给定文件中删除所需的属性,并将其余内容复制到临时文件中,但是会创建一个空白行。

<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> 
+0

它可能就是你打印出来的方式。空白行可能不存在于实际文档中。你如何解析/打印XML?另外,我认为你对XML属性和XML元素的概念有点模糊。 ''实际上是一个XML元素。 – adarshr 2012-04-10 14:34:44

回答

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

如何将我的文档转换为**文档**类型? – STF 2016-01-05 13:09:56