I need to query XML documents using XPath expressions in a Java application. I created the following class that accepts a file (the location of the XML document on the local hard drive) and the XPath request and should return the result of evaluating this request in this document.
import java.io.File;
import java.io.IOException;
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.XPathException;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class XPathResolver
{
public String resolveXPath(File xmlFile, String xpathExpr) throws XPathException, ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(xpathExpr);
return (String) expr.evaluate(doc, XPathConstants.STRING);
}
}
Suppose now that I have the following XML document.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document>
<DocumentFormat>Email</DocumentFormat>
<FileFormat>PDF</FileFormat>
</Document>
Evaluation of both /Document/FileFormat, and //FileFormatreturn PDF(as expected).
Suppose now that a document with namespace prefixes, for example, is as follows.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document xmlns:file="http://www.example.com/xml/file">
<DocumentFormat>Email</DocumentFormat>
<file:FileFormat>PDF</file:FileFormat>
</Document>
Now /Document/FileFormatreturns PDF, but //FileFormatreturns nothing.
Why does my code not return the expected result in the case of documents with namespace prefixes and how to fix it?