2012-03-26 114 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