Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Help required for filtering XML using DOM parser

  Asked By: Lloyd    Date: Oct 03    Category: Java    Views: 1015
  

I am really not sure if i
would sound stupid with the problem I am seeking help for. But still
optimistic about recieving an answer I am sending this mail.

I have this requirement of filtering out some sub-nodes from a XML
based on the values of attribute inside the sub-node. For eg

Input XML:
---------
<rootnode>
<mynode>
<myvalue>1</myvalue>
</mynode>
<mynode>
<myvalue>1</myvalue>
</mynode>
<mynode>
<myvalue>2</myvalue>
</mynode>
<mynode>
<myvalue>2</myvalue>
</mynode>
<mynode>
<myvalue>1</myvalue>
</mynode>
</rootnode>

Processing:
----------
Filter out all nodes which have myvalue=2

Output XML:
----------
<rootnode>
<mynode>
<myvalue>1</myvalue>
</mynode>
<mynode>
<myvalue>1</myvalue>
</mynode>
<mynode>
<myvalue>1</myvalue>
</mynode>
</rootnode>

Can anyone please suggest me the best way to do it ? I am a beginner
with DOM parsing. If you can please point me to available
documentation/ code snippents, that would be great.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Emily Brown     Answered On: Oct 03

Parse the XML file and get a reference to a Document
object. On the document object call the
getElementsByTagName(java.lang.String tagname) this
will return an NodeList. Iterate through this node
list and recreate a new DOM structure filtered values
and write the DOM structure back to the file system.
I can send you a sample code  if you tell me that
parser that you are using, is it Xerces or is it XML4J

 
Didn't find what you were looking for? Find more on Help required for filtering XML using DOM parser Or get search suggestion and latest updates.




Tagged: