我需要在 Java 应用程序中使用 XPath 表达式查询 XML 文档。我创建了以下类,它接受一个文件(本地硬盘驱动器上 XML 文档的位置)和一个 XPath 查询,并且应该返回在给定文档上评估给定查询的结果。
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);
}
}
现在假设我有以下 XML 文档。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document>
<DocumentFormat>Email</DocumentFormat>
<FileFormat>PDF</FileFormat>
</Document>
/Document/FileFormat
和 //FileFormat
的评估返回 PDF
(如预期)。然而,现在假设一个具有命名空间前缀的文档,如下所示。
<?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>
现在
/Document/FileFormat
返回 PDF
,但 //FileFormat
不返回任何内容。如果文档带有命名空间前缀,为什么我的代码不返回预期的输出,我该如何解决?
最佳答案
我用 JDK 1.7.0.51 尝试了你的例子,可以确认你的结果。起初这似乎有点奇怪,但 DocumentBuilderFactory
的默认行为是不知道命名空间的。
所以你必须首先打开它:
factory.setNamespaceAware(true);
然后对于第二个文档,XPath 表达式没有预期的结果。
您必须将表达式更改为:
/Document/file:FileFormat
和 //file:FileFormat
。在最后一步,您必须注册一个
NamespaceContext
实现,它将 XPath 表达式中使用的 namespace 前缀映射到 namespace URI。不幸的是,没有默认实现。public String resolveXPath(File xmlFile, String xpathExpr) throws XPathException, ParserConfigurationException, SAXException, IOException, XPathExpressionException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Turn namespace aware on
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
// Set the NamespaceContext
xpath.setNamespaceContext(new MyNamespaceContext());
XPathExpression expr = xpath.compile(xpathExpr);
return (String) expr.evaluate(doc, XPathConstants.STRING);
}
class MyNamespaceContext implements NamespaceContext {
private Map<String, String> ns;
private Map<String, String> nsReverted;
public MyNamespaceContext() {
ns = new TreeMap<String, String>();
// Default namespaces and prefixes according to the documentation
ns.put(XMLConstants.DEFAULT_NS_PREFIX, XMLConstants.NULL_NS_URI);
ns.put(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
ns.put(XMLConstants.XMLNS_ATTRIBUTE, XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
// Now our self defined namespace
ns.put("file", "http://www.example.com/xml/file");
nsReverted = new TreeMap<String, String>();
for(Entry<String, String> entry : ns.entrySet()) {
nsReverted.put(entry.getValue(), entry.getValue());
}
}
@Override
public String getNamespaceURI(String prefix) {
if(prefix == null) {
throw new IllegalArgumentException();
}
final String uri = ns.get(prefix);
return uri == null ? XMLConstants.NULL_NS_URI : uri;
}
@Override
public String getPrefix(String namespaceURI) {
if(namespaceURI == null) {
throw new IllegalArgumentException();
}
return nsReverted.get(namespaceURI);
}
@Override
public Iterator getPrefixes(String namespaceURI) {
return ns.keySet().iterator();
}
}
关于java - Java XPath 解析器对具有命名空间的文档的奇怪行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21727745/