zoukankan      html  css  js  c++  java
  • Java xpath example code THE RIGHT WAY

    http://www.xinotes.org/notes/note/1311/

    i have waste a afternoon time to find the java way to invoke xpath,

    java is evail

    Java xpath example code | Xinotes

        - Socrates
        Java xpath example code
        freyo

            Joined:
        07/27/2010
            Posts:
        122

        [Poor] [Fair] [Good] [Excellent] [Super!]  (Not rated)
        May 23, 2011 14:30:18    Last update: May 23, 2011 14:31:08
        There are two distinct ways to process XPath: with namespace and without namespace. The code is different depending on whether the parser is namespace aware.

            Code without namespace:

            import java.io.*;
            import javax.xml.parsers.*;
            import javax.xml.xpath.*;

            import org.w3c.dom.*;

            public class GetLicenseeName {
                public static void main(String[] args) throws Exception {
                if (args.length < 1) {
                    System.out.println("Usage: java GetLicenseeName <xml_file>");
                    return;
                }

                String xmlFile = args[0];

                DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
                InputStream in = new FileInputStream(xmlFile);
                Document doc = f.newDocumentBuilder().parse(in);
                in.close();

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

                NodeList nodes = (NodeList) xpath.evaluate(
                    "/test-license/licensee/name/text()",
                    doc,
                    XPathConstants.NODESET
                );
                System.out.println("Licensee name: " + nodes.item(0).getNodeValue());
                }
            }



            code with namespace:

            import java.io.*;
            import java.util.Iterator;
            import javax.xml.namespace.NamespaceContext;
            import javax.xml.parsers.*;
            import javax.xml.xpath.*;

            import org.w3c.dom.*;

            public class GetLicenseeNameNS {
                public static void main(String[] args) throws Exception {
                if (args.length < 1) {
                    System.out.println("Usage: java GetLicenseeNameNS <xml_file>");
                    return;
                }

                String xmlFile = args[0];

                DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
                // Now we are namespace aware!
                f.setNamespaceAware(true);
                InputStream in = new FileInputStream(xmlFile);
                Document doc = f.newDocumentBuilder().parse(in);
                in.close();

                XPath xpath = XPathFactory.newInstance().newXPath();
                xpath.setNamespaceContext(new NamespaceContext() {
                    public String getNamespaceURI(String prefix) {
                    // we know there's only one namespace uri, just return it!
                    return "http://www.notexisting.com/some_schema";
                    }

                    // this is not used by XPath
                    public String getPrefix(String namespaceURI) {
                    System.out.println("getPrefix: " + namespaceURI);
                    return null;
                    }

                    // this is not used by XPath
                    public Iterator getPrefixes(String namespaceURI) {
                    System.out.println("getPrefixes: " + namespaceURI);
                    return null;
                    }
                });

                String[] paths = {
                    "/test-license/licensee/name/text()",
                    "/p:test-license/p:licensee/p:name/text()",
                };

                for (String path: paths) {
                    NodeList nodes = (NodeList) xpath.evaluate(path, doc, XPathConstants.NODESET);
                    if (nodes.getLength() == 0) {
                    System.out.println(path + " does not work!");
                    }
                    else {
                    System.out.println(path + " works:");
                    System.out.println("Licensee name: " + nodes.item(0).getNodeValue());
                    }
                    System.out.println();
                }
                }
            }

            XML without namespace:

            <?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <test-license>
                <licensee>
                <name>Paid in Full</name>
                <address>1234 Main. St.</address>
                </licensee>

                <product>
                <name>No use</name>
                </product>
            </test-license>

            XML with namespace:

            <?xml version="1.0" encoding="UTF-8" standalone="no"?>
            <test-license xmlns="http://www.notexisting.com/some_schema">
                <licensee>
                <name>Paid in Full</name>
                <address>1234 Main. St.</address>
                </licensee>

                <product>
                <name>No use</name>
                </product>
            </test-license>


        The same XPath expression works for both XML files when the parser is not namespace aware.

        When the parser is namespace aware, you have to adjust the XPath accordingly depending on whether the XML has namespace declarations: "/test-license/licensee/name/text()" works for the XML file without namespace, while "/p:test-license/p:licensee/p:name/text()" works for the XML file with namespace.
        Share |
        | Comment  | Tags
        

  • 相关阅读:
    九个PHP有用功能
    ajax的上拉功能
    jquery 插件
    任务08(全体)——第一次半月学习总结
    任务02——安装 Intellj IDEA,编写一个简易四则运算小程序,并将代码提交到 GitHub
    任务01——谈谈对参与工作室的预期
    任务08
    任务03
    任务02
    任务01
  • 原文地址:https://www.cnblogs.com/lexus/p/2358464.html
Copyright © 2011-2022 走看看