使用JaVa从XML中删除XML节点

时间:2012-08-16 11:47:53

标签: java xml parsing nodes

我有一个XML文档

<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中删除第3和第5个ProductDetails

根据记录位置详情我必须删除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代码中激活它!

1 个答案:

答案 0 :(得分:0)

使用标准库。对不起,你必须阅读Java Tutorial

  

“要删除节点,请使用其父节点的removeChild方法。”