-3

I have a XML document

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

I have able to count the total number of records using Java as 5. I have few requirement in which i have to remove some records before processing further. Say for example I have to remove 3rd and 5th ProductDetails from the XML

Base on the record Position details I have to remove the 2 record

Final Output

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

Can some one please help be to active this in Java Code!

  • 2
    Show us what have you tried and where did you stuck and we'll help you to move on. I'd use DOM to create a document object model and then you can add/remove nodes in model. Then you can write DOM to file again. There is a tutorial googled right now: http://tutorials.jenkov.com/java-xml/dom.html – Piotr Gwiazda Aug 16 '12 at 11:55
  • See: http://stackoverflow.com/a/3717875/383861 – bdoughan Aug 16 '12 at 13:14

1 Answers1

0

Use the standard library. I'm sorry but you've got to read the Java Tutorial :

"To remove a node, you use its parent Node's removeChild method."

Jérôme Radix
  • 10,285
  • 4
  • 34
  • 40