I want to read following XML file:
<RootNode>
<Node id="1"> value1 </Node>
<Node id="2"> value2 </Node>
<Node id="3"> value3 </Node>
<Node id="4"> value4 </Node>
<Node1 id="1"> value11 </Node1>
<Node1 id="2"> value12 </Node2>
...
</RootNode>
Now depending on the Node id I want to fetch the value. Like if the Node name is Node
and id is 1
the value should be value1
and if Node name is Node1
and id is 2
then value should be value12
.
I'm able to get the elements with name Node
using this code:
try{
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("Node");
}
catch(Execption e){
e.printStacktrace();
}
How can I get the elements depending on the attribute(id
in this case) ?