import java.io.IOException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
public class XmlTest {
/**
* @param args
* @throws ParserConfigurationException
* @throws XPathExpressionException
* @throws IOException
* @throws SAXException
*/
public static void main(String[] args) throws ParserConfigurationException, XPathExpressionException, SAXException, IOException {
// TODO Auto-generated method stub
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("C://test.XML");
String strXpath = "//DataInfo[@Year='2010']/Return[@Type='1' and EndDate='2010-12-31']/Retrun[@Period='4']/Value/text()";
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr
= xpath.compile(strXpath);
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
}
}