<small id='1k9Au'></small><noframes id='1k9Au'>

    1. <tfoot id='1k9Au'></tfoot>

      1. <legend id='1k9Au'><style id='1k9Au'><dir id='1k9Au'><q id='1k9Au'></q></dir></style></legend>
      2. <i id='1k9Au'><tr id='1k9Au'><dt id='1k9Au'><q id='1k9Au'><span id='1k9Au'><b id='1k9Au'><form id='1k9Au'><ins id='1k9Au'></ins><ul id='1k9Au'></ul><sub id='1k9Au'></sub></form><legend id='1k9Au'></legend><bdo id='1k9Au'><pre id='1k9Au'><center id='1k9Au'></center></pre></bdo></b><th id='1k9Au'></th></span></q></dt></tr></i><div id='1k9Au'><tfoot id='1k9Au'></tfoot><dl id='1k9Au'><fieldset id='1k9Au'></fieldset></dl></div>
        • <bdo id='1k9Au'></bdo><ul id='1k9Au'></ul>

        Java中带有命名空间的XPath

        XPath with namespace in Java(Java中带有命名空间的XPath)

        <tfoot id='dZ56w'></tfoot>

              • <bdo id='dZ56w'></bdo><ul id='dZ56w'></ul>

                <small id='dZ56w'></small><noframes id='dZ56w'>

                <legend id='dZ56w'><style id='dZ56w'><dir id='dZ56w'><q id='dZ56w'></q></dir></style></legend>
              • <i id='dZ56w'><tr id='dZ56w'><dt id='dZ56w'><q id='dZ56w'><span id='dZ56w'><b id='dZ56w'><form id='dZ56w'><ins id='dZ56w'></ins><ul id='dZ56w'></ul><sub id='dZ56w'></sub></form><legend id='dZ56w'></legend><bdo id='dZ56w'><pre id='dZ56w'><center id='dZ56w'></center></pre></bdo></b><th id='dZ56w'></th></span></q></dt></tr></i><div id='dZ56w'><tfoot id='dZ56w'></tfoot><dl id='dZ56w'><fieldset id='dZ56w'></fieldset></dl></div>
                  <tbody id='dZ56w'></tbody>
                  本文介绍了Java中带有命名空间的XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想获取标签之间的所有内容,但由于 urn: 命名空间,我不知道该怎么做.

                  I would like to get all the content in between the tags but I do not know how to do this because of the urn: namespace.

                  <urn:ResponseStatus version="1.0" xmlns:urn="urn:camera-org">
                  
                  <urn:requestURL>/CAMERA/Streaming/status</urn:requestURL>
                  <urn:statusCode>4</urn:statusCode>
                  <urn:statusString>Invalid Operation</urn:statusString>
                  <urn:id>0</urn:id>
                  
                  </urn:ResponseStatus>
                  

                  有什么想法吗?

                  推荐答案

                  1. 简答:使用 XPath local-name().像这样: xPathFactory.newXPath().compile("//*[local-name()='requestURL']/text()"); 将返回 /CAMERA/Streaming/状态
                  2. 或者您可以实现一个 NamespaceContext 来映射命名空间名称和 URI,并在查询之前将其设置在 XPath 对象上.
                  3. 看看这篇博客文章,更新:文章已下架,您可以在webarchive
                  1. Short answer: use XPath local-name(). Like this: xPathFactory.newXPath().compile("//*[local-name()='requestURL']/text()"); will return /CAMERA/Streaming/status
                  2. Or you can implement a NamespaceContext that maps namespaces names and URIs and set it on the XPath object before querying.
                  3. Take a look at this blog article, Update: the article is down, you can see it on webarchive

                  解决方案 1 示例:

                  XPath xpath = XPathFactory.newInstance().newXPath();
                  String responseStatus = xpath.evaluate("//*[local-name()='ResponseStatus']/text()", document);
                  System.out.println("-> " + responseStatus);
                  

                  解决方案 2 示例:

                  // load the Document
                  Document document = ...;
                  NamespaceContext ctx = new NamespaceContext() {
                      public String getNamespaceURI(String prefix) {
                          return prefix.equals("urn") ? "urn:camera-org" : null; 
                      }
                      public Iterator getPrefixes(String val) {
                          return null;
                      }
                      public String getPrefix(String uri) {
                          return null;
                      }
                  };
                  XPath xpath = XPathFactory.newInstance().newXPath();
                  xpath.setNamespaceContext(ctx);
                  String responseStatus = xpath.evaluate("//urn:ResponseStatus/text()", document);
                  System.out.println("-> " + responseStatus);
                  

                  编辑

                  这是一个完整的例子,它正确地检索了元素:

                  This is a complete example, it correctly retrieve the element:

                  String xml = "<urn:ResponseStatus version="1.0" xmlns:urn="urn:camera-org">
                  " + //
                          "
                  " + //
                          "<urn:requestURL>/CAMERA/Streaming/status</urn:requestURL>
                  " + //
                          "<urn:statusCode>4</urn:statusCode>
                  " + //
                          "<urn:statusString>Invalid Operation</urn:statusString>
                  " + //
                          "<urn:id>0</urn:id>
                  " + //
                          "
                  " + //
                          "</urn:ResponseStatus>";
                  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                  factory.setNamespaceAware(true);
                  DocumentBuilder builder = factory.newDocumentBuilder();
                  Document doc = builder.parse(new java.io.ByteArrayInputStream(xml.getBytes()));
                  XPath xpath = XPathFactory.newInstance().newXPath();
                  xpath.setNamespaceContext(new NamespaceContext() {
                      public String getNamespaceURI(String prefix) {
                          return prefix.equals("urn") ? "urn:camera-org" : null;
                      }
                  
                      public Iterator<?> getPrefixes(String val) {
                          return null;
                      }
                  
                      public String getPrefix(String uri) {
                          return null;
                      }
                  });
                  XPathExpression expr = xpath.compile("//urn:ResponseStatus");
                  Object result = expr.evaluate(doc, XPathConstants.NODESET);
                  NodeList nodes = (NodeList) result;
                  for (int i = 0; i < nodes.getLength(); i++) {
                      Node currentItem = nodes.item(i);
                      System.out.println("found node -> " + currentItem.getLocalName() + " (namespace: " + currentItem.getNamespaceURI() + ")");
                  }
                  

                  这篇关于Java中带有命名空间的XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  <i id='5XGjy'><tr id='5XGjy'><dt id='5XGjy'><q id='5XGjy'><span id='5XGjy'><b id='5XGjy'><form id='5XGjy'><ins id='5XGjy'></ins><ul id='5XGjy'></ul><sub id='5XGjy'></sub></form><legend id='5XGjy'></legend><bdo id='5XGjy'><pre id='5XGjy'><center id='5XGjy'></center></pre></bdo></b><th id='5XGjy'></th></span></q></dt></tr></i><div id='5XGjy'><tfoot id='5XGjy'></tfoot><dl id='5XGjy'><fieldset id='5XGjy'></fieldset></dl></div>
                  1. <legend id='5XGjy'><style id='5XGjy'><dir id='5XGjy'><q id='5XGjy'></q></dir></style></legend>

                      1. <tfoot id='5XGjy'></tfoot>

                        <small id='5XGjy'></small><noframes id='5XGjy'>

                            <tbody id='5XGjy'></tbody>
                          • <bdo id='5XGjy'></bdo><ul id='5XGjy'></ul>