Java API编写自己的NamespaceContext(5)

http://www.itjxue.com  2015-08-06 23:17  来源:未知  点击次数: 

 

从文档及其所有元素读取名称空间并对之进行缓存

 

此版本将从这个 XML 文件读取所有名称空间声明。现在,即便是前缀 science 上的 XPath 也是有效的。但是有一种情况让此版本有些复杂:如果一个前缀重载(在不同 URI 上的嵌套元素内声明),所找到的最后一个将会 “胜出”。在实际中,这通常不成问题。

 

在本例中,NamespaceContext 的使用与前一个示例相同。构造函数内的布尔值 toplevelOnly 必须被设置为 false。

 

清单 13. 具有缓存了的名称空间解析的示例 4(面向所有级别)

private static void example4(Document example)
            throws XPathExpressionException, TransformerException {
        sysout("\n*** Fourth example - namespaces all levels cached ***");

        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new UniversalNamespaceCache(example, false));
...
        NodeList result1 = (NodeList) xPath.evaluate(
                "books:booklist/science:book", example, XPathConstants.NODESET);
...
        NodeList result2 = (NodeList) xPath.evaluate(
                "books:booklist/fiction:book", example, XPathConstants.NODESET);
...
        String result = xPath.evaluate(
                "books:booklist/fiction:book[1]/:author", example);
...
    }

 

其输出结果如下:

 

清单 14. 示例 4 的输出

*** Fourth example - namespaces all levels cached ***
The list of the cached namespaces:
prefix science: uri http://univNaSpResolver/sciencebook
prefix DEFAULT: uri http://univNaSpResolver/book
prefix fiction: uri http://univNaSpResolver/fictionbook
prefix books: uri http://univNaSpResolver/booklist
Now the use of the science prefix works as well:
--> books:booklist/science:book
Number of Nodes: 1
<?xml version="1.0" encoding="UTF-8"?>
  <science:book xmlns:science="http://univNaSpResolver/sciencebook">
    <title xmlns="http://univNaSpResolver/book">Learning XPath</title>
    <author xmlns="http://univNaSpResolver/book">Michael Schmidt</author>
  </science:book>
The fiction namespace is resolved:
--> books:booklist/fiction:book
Number of Nodes: 2
<?xml version="1.0" encoding="UTF-8"?>
  <fiction:book xmlns:fiction="http://univNaSpResolver/fictionbook">
    <title xmlns="http://univNaSpResolver/book">Faust I</title>
    <author xmlns="http://univNaSpResolver/book">Johann Wolfgang von Goethe</author>
  </fiction:book>
<?xml version="1.0" encoding="UTF-8"?>
  <fiction:book xmlns:fiction="http://univNaSpResolver/fictionbook">
    <title xmlns="http://univNaSpResolver/book">Faust II</title>
    <author xmlns="http://univNaSpResolver/book">Johann Wolfgang von Goethe</author>
  </fiction:book>
The default namespace works also:
--> books:booklist/fiction:book[1]/:author
Johann Wolfgang von Goethe

 

结束语

 

实现名称空间解析,有几种方式可供选择,这些方式大都好于硬编码的实现方式:

 

•如果示例很小并且所有名称空间均位于顶部元素内,指派到此文档的方式将会十分有效。

•如果 XML 文件较大且具有深层嵌套和多个 XPath 求值,最好是缓存名称空间的列表。

•但是如果您无法控制 XML 文件,并且别人可以发送给您任何前缀,最好是独立于他人的选择。您可以编码实现您自己的名称空间解析,如示例 1 (HardcodedNamespaceResolver)所示,并将它们用于您的 XPath 表达式。

在上述这些情况下,解析自此 XML 文件的 NamespaceContext 能够让您的代码更少、并且更为通用。

 

从文档读取名称空间

 

名称空间及其前缀均存档在此 XML 文件内,因此可以从那里使用它们。实现此目的的最为简单的方式是将这个查找指派给该文档。

 

清单 7. 从文档直接进行名称空间解析

public class UniversalNamespaceResolver implements NamespaceContext {
    // the delegate
    private Document sourceDocument;

    /**
     * This constructor stores the source document to search the namespaces in
     * it.
     * 
     * @param document
     *            source document
     */
    public UniversalNamespaceResolver(Document document) {
        sourceDocument = document;
    }

    /**
     * The lookup for the namespace uris is delegated to the stored document.
     * 
     * @param prefix
     *            to search for
     * @return uri
     */
    public String getNamespaceURI(String prefix) {
        if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
            return sourceDocument.lookupNamespaceURI(null);
        } else {
            return sourceDocument.lookupNamespaceURI(prefix);
        }
    }

    /**
     * This method is not needed in this context, but can be implemented in a
     * similar way.
     */
    public String getPrefix(String namespaceURI) {
        return sourceDocument.lookupPrefix(namespaceURI);
    }

    public Iterator getPrefixes(String namespaceURI) {
        // not implemented yet
        return null;
    }

}

 

请注意如下这些事项:

 

•如果文档在使用 XPath 前已更改,那么此更改还将反应在名称空间的这个查找上,因为指派是在需要的时候通过使用文档的当前版本完成的。

•对名称空间或前缀的查找在所用节点的祖先节点完成,在我们的例子中,即节点 sourceDocument。这意味着,借助所提供的代码,您只需在根节点上声明此名称空间。在我们的示例中,名称空间 science 没有被找到。

•此查找在 XPath 求值时被调用,因此它会消耗一些额外的时间。

如下是示例代码:

 

清单 8. 从文档直接进行名称空间解析的示例 2

private static void example2(Document example)
            throws XPathExpressionException, TransformerException {
        sysout("\n*** Second example - namespacelookup delegated to document ***");

        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new UniversalNamespaceResolver(example));

        try {
...
            NodeList result1 = (NodeList) xPath.evaluate(
                    "books:booklist/science:book", example,
                    XPathConstants.NODESET);
...
        } catch (XPathExpressionException e) {
...
        }
...
        NodeList result2 = (NodeList) xPath.evaluate(
                "books:booklist/fiction:book", example, XPathConstants.NODESET);
...
        String result = xPath.evaluate(
                "books:booklist/fiction:book[1]/:author", example);
...
    }

 

此示例的输出为:

 

清单 9. 示例 2 的输出

*** Second example - namespacelookup delegated to document ***
Try to use the science prefix: no result
--> books:booklist/science:book
The resolver only knows namespaces of the first level!
To be precise: Only namespaces above the node, passed in the constructor.
The fiction namespace is such a namespace:
--> books:booklist/fiction:book
Number of Nodes: 2
<?xml version="1.0" encoding="UTF-8"?>
  <fiction:book xmlns:fiction="http://univNaSpResolver/fictionbook">
    <title xmlns="http://univNaSpResolver/book">Faust I</title>
    <author xmlns="http://univNaSpResolver/book">Johann Wolfgang von Goethe</author>
  </fiction:book>
<?xml version="1.0" encoding="UTF-8"?>
  <fiction:book xmlns:fiction="http://univNaSpResolver/fictionbook">
    <title xmlns="http://univNaSpResolver/book">Faust II</title>
    <author xmlns="http://univNaSpResolver/book">Johann Wolfgang von Goethe</author>
  </fiction:book>
The default namespace works also:
--> books:booklist/fiction:book[1]/:author
Johann Wolfgang von Goethe

 

正如输出所示,在 book 元素上声明的、具有前缀 science 的名称空间并未被解析。求值方法抛出了一个 XPathExpressionException。要解决这个问题,需要从文档提取节点 science:book 并将此节点用作代表(delegate)。但是这将意味着对此文档要进行额外的解析,而且也不优雅。

 

提供名称空间解析的必要性

 

如果 XML 使用了名称空间,若不提供 NamespaceContext,那么 XPath 表达式将会失效。清单 2 中的示例 0 充分展示了这一点。其中的 XPath 对象在所加载的 XML 文档之上构建和求值。首先,尝试不用任何名称空间前缀(result1)编写此表达式。之后,再用名称空间前缀(result2)编写此表达式。

 

清单 2. 无名称空间解析的示例 0

private static void example0(Document example)
            throws XPathExpressionException, TransformerException {
        sysout("\n*** Zero example - no namespaces provided ***");

        XPath xPath = XPathFactory.newInstance().newXPath();

...
        NodeList result1 = (NodeList) xPath.evaluate("booklist/book", example,
                XPathConstants.NODESET);
...
        NodeList result2 = (NodeList) xPath.evaluate(
                "books:booklist/science:book", example, XPathConstants.NODESET);
...
    }

 

输出如下所示。

 

清单 3. 示例 0 的输出

*** Zero example - no namespaces provided ***
First try asking without namespace prefix:
--> booklist/book
Result is of length 0
Then try asking with namespace prefix:
--> books:booklist/science:book
Result is of length 0
The expression does not work in both cases.

 

在两种情况下,XPath 求值并不返回任何节点,而且也没有任何异常。XPath 找不到节点,因为缺少前缀到 URI 的映射。

 

硬编码的名称空间解析

 

也可以以硬编码的值来提供名称空间,类似于 清单 4 中的类:

 

清单 4. 硬编码的名称空间解析

public class HardcodedNamespaceResolver implements NamespaceContext {

    /**
     * This method returns the uri for all prefixes needed. Wherever possible
     * it uses XMLConstants.
     * 
     * @param prefix
     * @return uri
     */
    public String getNamespaceURI(String prefix) {
        if (prefix == null) {
            throw new IllegalArgumentException("No prefix provided!");
        } else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
            return "http://univNaSpResolver/book";
        } else if (prefix.equals("books")) {
            return "http://univNaSpResolver/booklist";
        } else if (prefix.equals("fiction")) {
            return "http://univNaSpResolver/fictionbook";
        } else if (prefix.equals("technical")) {
            return "http://univNaSpResolver/sciencebook";
        } else {
            return XMLConstants.NULL_NS_URI;
        }
    }

    public String getPrefix(String namespaceURI) {
        // Not needed in this context.
        return null;
    }

    public Iterator getPrefixes(String namespaceURI) {
        // Not needed in this context.
        return null;
    }

}

 

请注意名称空间 http://univNaSpResolver/sciencebook 被绑定到了前缀 technical(不是之前的 science)。结果将可以在随后的 示例(清单 6)中看到。在 清单 5 中,使用此解析器的代码还使用了新的前缀。

 

清单 5. 具有硬编码名称空间解析的示例 1

private static void example1(Document example)
            throws XPathExpressionException, TransformerException {
        sysout("\n*** First example - namespacelookup hardcoded ***");

        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new HardcodedNamespaceResolver());

...
        NodeList result1 = (NodeList) xPath.evaluate(
                "books:booklist/technical:book", example,
                XPathConstants.NODESET);
...
        NodeList result2 = (NodeList) xPath.evaluate(
                "books:booklist/fiction:book", example, XPathConstants.NODESET);
...
        String result = xPath.evaluate("books:booklist/technical:book/:author",
                example);
...
    }

 

如下是此示例的输出。

 

清单 6. 示例 1 的输出

*** First example - namespacelookup hardcoded ***
Using any namespaces results in a NodeList:
--> books:booklist/technical:book
Number of Nodes: 1
<?xml version="1.0" encoding="UTF-8"?>
  <science:book xmlns:science="http://univNaSpResolver/sciencebook">
    <title xmlns="http://univNaSpResolver/book">Learning XPath</title>
    <author xmlns="http://univNaSpResolver/book">Michael Schmidt</author>
  </science:book>
--> books:booklist/fiction:book
Number of Nodes: 2
<?xml version="1.0" encoding="UTF-8"?>
  <fiction:book xmlns:fiction="http://univNaSpResolver/fictionbook">
    <title xmlns="http://univNaSpResolver/book">Faust I</title>
    <author xmlns="http://univNaSpResolver/book">Johann Wolfgang von Goethe</author>
  </fiction:book>
<?xml version="1.0" encoding="UTF-8"?>
  <fiction:book xmlns:fiction="http://univNaSpResolver/fictionbook">
    <title xmlns="http://univNaSpResolver/book">Faust II</title>
    <author xmlns="http://univNaSpResolver/book">Johann Wolfgang von Goethe</author>
  </fiction:book>
The default namespace works also:
--> books:booklist/technical:book/:author
Michael Schmidt

 

如您所见,XPath 现在找到了节点。好处是您可以如您所希望的那样重命名前缀,我对前缀 science 就是这么做的。XML 文件包含前缀 science,而 XPath 则使用了另一个前缀 technical。由于这些 URI 都是相同的,所以节点均可被 XPath 找到。不利之处是您必须要在多个地方(XML、XSD、 XPath 表达式和此名称空间的上下文)维护名称空间。

(责任编辑:IT教学网)

更多

推荐Javascript/Ajax文章