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
        

  • 相关阅读:
    【转载】如果你看完这篇文章还不懂计算机时间,那就掐死我吧
    记录 C#中 LINQ 和 SQL 语句 的一些操作数据集合
    记录mysql 存储过程中循环临时表
    记录一个有趣的dotnet开源库。
    将Quartz.Net用于ASP.NET Core 3.0应用程序中的,并实现通过依赖注入获取其他服务
    web api中接收 复杂类型数组参数(对象数组参数)
    网页支付宝支付,通过form表单提交,在苹果手机上无法跳转
    阿里云服务器上搭建FTP服务器,连接时出现:读取目录列表失败的解决办法
    css选择器及float(浮动)
    盒子四大元素
  • 原文地址:https://www.cnblogs.com/lexus/p/2358464.html
Copyright © 2011-2022 走看看