• <bdo id='9Miyu'></bdo><ul id='9Miyu'></ul>
  • <i id='9Miyu'><tr id='9Miyu'><dt id='9Miyu'><q id='9Miyu'><span id='9Miyu'><b id='9Miyu'><form id='9Miyu'><ins id='9Miyu'></ins><ul id='9Miyu'></ul><sub id='9Miyu'></sub></form><legend id='9Miyu'></legend><bdo id='9Miyu'><pre id='9Miyu'><center id='9Miyu'></center></pre></bdo></b><th id='9Miyu'></th></span></q></dt></tr></i><div id='9Miyu'><tfoot id='9Miyu'></tfoot><dl id='9Miyu'><fieldset id='9Miyu'></fieldset></dl></div>

  • <small id='9Miyu'></small><noframes id='9Miyu'>

      1. <tfoot id='9Miyu'></tfoot>

      2. <legend id='9Miyu'><style id='9Miyu'><dir id='9Miyu'><q id='9Miyu'></q></dir></style></legend>

        Java中带有命名空间的XPath

        时间:2023-09-30

          <tbody id='yfSJq'></tbody>

        • <tfoot id='yfSJq'></tfoot>
            <bdo id='yfSJq'></bdo><ul id='yfSJq'></ul>

              1. <legend id='yfSJq'><style id='yfSJq'><dir id='yfSJq'><q id='yfSJq'></q></dir></style></legend>

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

                  <i id='yfSJq'><tr id='yfSJq'><dt id='yfSJq'><q id='yfSJq'><span id='yfSJq'><b id='yfSJq'><form id='yfSJq'><ins id='yfSJq'></ins><ul id='yfSJq'></ul><sub id='yfSJq'></sub></form><legend id='yfSJq'></legend><bdo id='yfSJq'><pre id='yfSJq'><center id='yfSJq'></center></pre></bdo></b><th id='yfSJq'></th></span></q></dt></tr></i><div id='yfSJq'><tfoot id='yfSJq'></tfoot><dl id='yfSJq'><fieldset id='yfSJq'></fieldset></dl></div>

                  本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Spring Data JDBC - 多对一关系 下一篇:使用名称空间和前缀解组 JAXB

                  相关文章

                  最新文章

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

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

                      <bdo id='Wrj6i'></bdo><ul id='Wrj6i'></ul>