jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  ibm xml专区中对XPATH的一个好文,http://www.ibm.com/developerworks/cn/xml/x-xpathjava/

主要小结如下:
1 JDK 1.5中已经自带了很好的import javax.xml.xpath.XPathFactory;
了,用1.5吧

2 核心代码
  public XPathEvaluator(String xmlFilename) throws IOException {
    try {
      // Convert filename into a DOM tree
      DocumentBuilderFactory domFactory =
        DocumentBuilderFactory.newInstance();
      domFactory.setNamespaceAware(true);
      DocumentBuilder builder = domFactory.newDocumentBuilder();
      this.domTree = builder.parse(xmlFilename);
    } catch (SAXException e) {
      throw new IOException("Error in document parsing: " + e.getMessage());
    } catch (ParserConfigurationException e) {
      throw new IOException("Error in configuring parser: " + e.getMessage());
    }
  }

  public NodeList evaluateXPath(String xpathString) throws IOException {
    try {
      XPathFactory factory = XPathFactory.newInstance();
      XPath xpath = factory.newXPath();
      return (NodeList)xpath.evaluate(
        xpathString, domTree, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
      throw new IOException("Error evaluating XPath: " + e.getMessage());
    }
  }

  public static void main(String[] args) {
    try {
      if (args.length != 1) {
        System.err.println("Usage: java ibm.dw.xpath.XPathEvaluator " +
          "[XML filename]");
        System.exit(1);
      }
      XPathEvaluator evaluator = new XPathEvaluator(args[0]);
      String xpathString = "//target[@name='init']/property[" +
                           "starts-with(@name, 'parser')]";
      NodeList results = evaluator.evaluateXPath(xpathString);
      for (int i=0; i<results.getLength(); i++) {
        Node node = results.item(i);
        System.out.print("Result: ");
        switch (node.getNodeType()) {
          case Node.ELEMENT_NODE: System.out.println("Element node named " +
                                    node.getNodeName());
                                  break;
          case Node.ATTRIBUTE_NODE: System.out.println(
                                     "Attribute node named " +
                                       node.getNodeName() + " with value '" +
                                       node.getNodeValue() + "'");
                                  break;
          case Node.TEXT_NODE:    System.out.println("Text: '" +
                                    node.getNodeValue() + "'");
                                  break;
          default: System.out.println(node);
        }
      }

  

  可见其基本步骤为,1 读入一个XML

                         2 建立DOMTREE,即:
                             DocumentBuilderFactory domFactory =
         DocumentBuilderFactory.newInstance();
       domFactory.setNamespaceAware(true);
       DocumentBuilder builder = domFactory.newDocumentBuilder();
       this.domTree = builder.parse(xmlFilename);

                        3 String xpathString = "//target[@name='init']/property[" +
            "starts-with(@name, 'parser')]";
     NodeList results = evaluator.evaluateXPath(xpathString);

    这里向该方法传入xpath.其中evaluatexpath方法的功能为把domtree中用xpath解析后,返回一个nodelist,然后再解析.

             4 建立XPATH工厂
                 XPathFactory factory = XPathFactory.newInstance();
      XPath xpath = factory.newXPath();
      return (NodeList)xpath.evaluate(
        xpathString, domTree, XPathConstants.NODESET);

    
XPathFactory是一个抽象工厂.

抽象工厂设计模式使得这一种 API 能够支持不同的对象模型,如 DOM、JDOM 和 XOM.

为了选择不同的模型,需要向XPathFactory.newInstance()方法传递标识对象模型的统一资源标识符(URI).
目前只支持DOM.

    之后创建xpath对象.

   之后应用xpath表达式
   其中evaluate() 方法被声明为返回 Object,实际返回什么依赖于 XPath 表达式的结果以及要求的类型

一般来说,XPath与Java的映射关系是:


number 映射为 java.lang.Double
string 映射为 java.lang.String
boolean 映射为 java.lang.Boolean
node-set 映射为 org.w3c.dom.NodeList


在 Java 中计算 XPath 表达式时,第二个参数(XPathConstants常量)指定需要的返回类型.有五种可能,都在 javax.xml.xpath.XPathConstants 类中命名了常量:

XPathConstants.NODESET
XPathConstants.BOOLEAN
XPathConstants.NUMBER
XPathConstants.STRING
XPathConstants.NODE

XPathConstants.NODE提示:

最后一个 XPathConstants.NODE 实际上没有匹配的 XPath 类型.只有知道 XPath 表达式只返回一个节点或者只需要一个节点时才使用它.如果 XPath 表达式返回了多个节点并且指定了 XPathConstants.NODE,则 evaluate() 按照文档顺序返回第一个节点.如果 XPath 表达式选择了一个空集并指定了 XPathConstants.NODE,则 evaluate() 返回 null.


注意:如果不能完成要求的转换,evaluate()将抛出 XPathException

  最后,可以转换为DOM的NodeList进行解析.

一个更典型的小例子如下:

        DocumentFactory factory=DocumentFactory.newInstance();

  factory.setNamespaceAware(true); 

        DocumentBuilder builder=factory.newDocumentBuilder();

         Document doc=builder.parse("src/books.xml");

     XpathFactory xpathfactory=XpathFactory.newInstance();

    Xpath xpath=xpathfactory.newXpath();

   XPathExpression pathExpression = xpath   

                                .compile("//book[author='TEST']/title/text()");   

   Object result=pathExpression.evalucate(doc,XPathConstants.NODESET);

  NodeList nodes=(NodeList)result;

 for (i=0;i<=nodes.getLenth();i++)

   System.out.println(nodes.item(i).getNodevalue):

posted on 2008-09-14 08:29  jackyrong的世界  阅读(1017)  评论(0编辑  收藏  举报