2012-08-16 31 views
-3

刪除XML節點從XML我有一個XML文檔使用Java

<ns1:Product> 
<ns1:ProductDetails> 
    <ns1:StationID>1</ns1:StationID> 
    <ns1:ProductGroup>1</ns1:ProductGroup> 
    <ns1:AccountCode>1</ns1:AccountCode> 
    <ns1:Amount>1345</ns1:Amount> 
    </ns1:ProductDetails> 
<ns1:ProductDetails> 
    <ns1:StationID>1</ns1:StationID> 
    <ns1:ProductGroup>1</ns1:ProductGroup> 
    <ns1:AccountCode>2</ns1:AccountCode> 
    <ns1:Amount>35471</ns1:Amount> 
    </ns1:ProductDetails> 
<ns1:ProductDetails> 
    <ns1:StationID>1</ns1:StationID> 
    <ns1:ProductGroup>1</ns1:ProductGroup> 
    <ns1:AccountCode>3</ns1:AccountCode> 
    <ns1:Amount>31897</ns1:Amount> 
    </ns1:ProductDetails> 
<ns1:ProductDetails> 
    <ns1:StationID>1</ns1:StationID> 
    <ns1:ProductGroup>1</ns1:ProductGroup> 
    <ns1:AccountCode>4</ns1:AccountCode> 
    <ns1:Amount>143647</ns1:Amount> 
    </ns1:ProductDetails> 
<ns1:ProductDetails> 
    <ns1:StationID>1</ns1:StationID> 
    <ns1:ProductGroup>1</ns1:ProductGroup> 
    <ns1:AccountCode>5</ns1:AccountCode> 
    <ns1:Amount>34531</ns1:Amount> 
</ns1:ProductDetails> 

我能數的使用Java記錄的總數爲5。我有,我有幾個來要求在進一步處理前刪除一些記錄。例如說我必須從XML除去第三和第五產品詳細的記錄位置信息

基地我不得不刪除2記錄

最終輸出

<ns1:Product> 
<ns1:ProductDetails> 
    <ns1:StationID>1</ns1:StationID> 
    <ns1:ProductGroup>1</ns1:ProductGroup> 
    <ns1:AccountCode>1</ns1:AccountCode> 
    <ns1:Amount>1345</ns1:Amount> 
</ns1:ProductDetails> 
<ns1:ProductDetails> 
    <ns1:StationID>1</ns1:StationID> 
    <ns1:ProductGroup>1</ns1:ProductGroup> 
    <ns1:AccountCode>2</ns1:AccountCode> 
    <ns1:Amount>35471</ns1:Amount> 
</ns1:ProductDetails> 
<ns1:ProductDetails> 
    <ns1:StationID>1</ns1:StationID> 
    <ns1:ProductGroup>1</ns1:ProductGroup> 
    <ns1:AccountCode>4</ns1:AccountCode> 
    <ns1:Amount>143647</ns1:Amount> 
    </ns1:ProductDetails> 
</ns1:Product> 

可有一個人請幫助是在Java代碼中激活此功能!

+2

向我們展示您嘗試了哪些方法,以及您卡在哪裏,我們將幫助您繼續前進。我會使用DOM創建一個文檔對象模型,然後你可以添加/刪除模型中的節點。然後,您可以再次將DOM寫入文件。現在有一個教程使用Google搜索:http://tutorials.jenkov.com/java-xml/dom.html – 2012-08-16 11:55:24

+0

請參閱:http://stackoverflow.com/a/3717875/383861 – 2012-08-16 13:14:11

回答

0

使用標準庫。我很抱歉,但您必須閱讀Java Tutorial

「要刪除節點,請使用其父節點的removeChild方法。」