Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a node if parent node and child node has same name?

Tags:

java

xml

I am working on a project in which I have to operate XML files using java. Earlier I was getting nodes by using node name, ie. getElementsByTagName(). Now a problem arises when I have an xml file with parent node and child node with same name.
How can I distinguish between them.

This a short sample of my xml file.

 <deviceparameters>
     <parameter>   // parent
        <name>ABC</name> 
        <parameter>Yes</parameter> // child with same name
        <value>20</value> 
     </parameter>
 </deviceparameters>

Thanks in advance.

like image 211
Sachin Mhetre Avatar asked Dec 07 '25 03:12

Sachin Mhetre


1 Answers

Start with finding deviceparameters by name, and then find a named child parameter which is your first one, then then parameter child of that. The Javadoc will tell you the methods to use to find a named child (you may have to loop through the child elements).

like image 107
Francis Upton IV Avatar answered Dec 08 '25 17:12

Francis Upton IV