java - 具有 namespace 的文档的 Java XPath 解析器的奇怪行为

标签 java xml xpath xml-namespaces descendant-or-self

我需要在 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 - 具有 namespace 的文档的 Java XPath 解析器的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21727745/

相关文章:

java - 打印 JavaFX WebView

html - XML 中 "Unit Separator"的最佳值是多少?

java - JAXB : Read non-static attributes into a collection

python - javascript __doPostBack 的网页抓取在 td 中包含 href

java - 如何使用 ElementWalker 动态地将三元组添加到 Jena 查询?

java - 简单的 JasperReport 查询

java - Android Edittext行和字符限制

xml - 如何在 XQuery 中检查元素是否包含多个单独的单词?

xpath - 如何检查节点是否在表单标签内?

xml - xpath查询以处理XML输出