[英]Weird behaviour of Java XPath resolver for documents with namespaces
我需要在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表达式中使用的名称空间前缀映射到名称空间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();
}
}
“Now / Document / FileFormat返回PDF” - 鉴于你向我们展示了什么,它不应该。
要使用XPath搜索命名空间节点,您必须在XPath中使用前缀并告诉XPath引擎这些前缀引用哪些命名空间,或者通过在localname和namespace-uri上显式匹配来克服这些命名空间。
请参阅https://stackoverflow.com/questions/6390339/how-to-query-xml-using-namespaces-in-java-with-xpath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.