Zenoe
asked on
Extract Tags from XML using org.w3c.dom
I have an xml that looks something like the below. I am using Java. I have parsed it. (There can be an infinite number of <Result> tags as well as an infinite number of <Return> tags.)
<TestXML>
<FirstStuff>blah</FirstStu ff>
<MoreStuff>blah blah</MoreStuff>
<Result>1234</Result>
<Result>5678</Result>
<Return>
<Comment>This one worked</Comment>
<Pool>1234</Pool>
</Return>
<Return>
<Comment>This one worked better</Comment>
<Pool>5678</Pool>
</Return>
</TestXML>
This is a piece of code that I have used:
final Document document = xmlObject.getXmlAsDocument ();
final NodeList nodeList = document.getElementsByTagN ame("Resul t");
final Vector results = new Vector();
for (int i = 0; i < nodeList.getLength(); i++) {
final Node node = nodeList.item(i);
final String nodeName = node.getNodeName();
final Node nodeValue = node.getChildNodes().item( 0);
results.add(nodeValue.getN odeValue() );
}
This works fine. But now I don't need to get the results tag anymore, but the two tags inside the Return tag. And I need to associate the comment with the pool, so I need to get them together so I can put them in some object before adding them to the Vector. I am very unfamiliar with how this work and am struggling to read the tags inside the Results tag. I tried the getChildren and getSibling thing...but I don't think I am using it in the right way. Can someone help me ??
<TestXML>
<FirstStuff>blah</FirstStu
<MoreStuff>blah blah</MoreStuff>
<Result>1234</Result>
<Result>5678</Result>
<Return>
<Comment>This one worked</Comment>
<Pool>1234</Pool>
</Return>
<Return>
<Comment>This one worked better</Comment>
<Pool>5678</Pool>
</Return>
</TestXML>
This is a piece of code that I have used:
final Document document = xmlObject.getXmlAsDocument
final NodeList nodeList = document.getElementsByTagN
final Vector results = new Vector();
for (int i = 0; i < nodeList.getLength(); i++) {
final Node node = nodeList.item(i);
final String nodeName = node.getNodeName();
final Node nodeValue = node.getChildNodes().item(
results.add(nodeValue.getN
}
This works fine. But now I don't need to get the results tag anymore, but the two tags inside the Return tag. And I need to associate the comment with the pool, so I need to get them together so I can put them in some object before adding them to the Vector. I am very unfamiliar with how this work and am struggling to read the tags inside the Results tag. I tried the getChildren and getSibling thing...but I don't think I am using it in the right way. Can someone help me ??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
First, get elements by tag name from "Return" tag.
Now you have a node list with all Return nodes.
To get the tags inside you have to get the child nodes (getChildNodes()) from the items in your for loop.
With getChildNodes you get another node list.
Iterate over it to get the Comment and Pool tags. To get the text try getTextContent on the nodes.