[code=Java]package com.example;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) throws XPathExpressionException,
ParserConfigurationException, SAXException, IOException {
InputStream is = Test.class.getResourceAsStream("foo.xml");
read(is);
}
public static void read(InputStream is)
throws ParserConfigurationException, SAXException, IOException,
XPathExpressionException {
DocumentBuilder db = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document dom = db.parse(is);
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList result = (NodeList) xpath.evaluate("//*[@name='Add']", dom,
XPathConstants.NODESET);
result.toString();
for (int i = 0; i < result.getLength(); i++) {
Node node = result.item(i);
System.out.println(i);
for (int o = 0; o < node.getChildNodes().getLength(); o++) {
Node child = node.getChildNodes().item(o);
if (child instanceof Element)
System.out.println("Add:" + node.getChildNodes().item(o));
}
}
}
}[/code]