I need to get data from an XML and I'm using XPath, quite new to it, though I'm liking it.
I'm retrieving some nodes based on their attributes like this:
/cesAlign/linkGrp[@targType='s']
Now I'd like to get the value of another attribute in the node:
/cesAlign/linkGrp[@targType='s']/@fromDoc
However, this returns the first hit only. I'd like to return the attribute of all nodes containing targType ='s'
I was thinking of looping over the nodelist and then reading the attribute... something like this:
expr = xpath.compile("/cesAlign/linkGrp[@targType='s']/@fromDoc");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
int i = 0;
for (i = 0; i < nl.getLength(); i++) {
expr = xpath.compile("/@fromDoc");
System.out.println((String) expr.evaluate(nl, XPathConstants.STRING));
}
But I'm not sure if there's a better and more elegant way to do this.
Here's a sample XML:
<cesAlign version="1.0">
<linkGrp targType="s" toDoc="mt/C2004310.01029701.xml.gz" fromDoc="en/C2004310.01029701.xml.gz">
<linkGrp targType="s" toDoc="mt/C2004310.01029702.xml.gz" fromDoc="en/C2004310.01029702.xml.gz">
</cesAlign>
Thanks!