1

我是XPath的新手,遇到以下问题:

我有一个从 web 服务接收数据的 Java 方法,这些数据位于 XML 文档中,因此我必须使用 XPath 在此 XML 结果文档中获取特定值。

特别是我知道这是我的 Web 服务(Web 服务响应)提供的整个 XML 输出:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <getConfigSettingsResponse xmlns="http://tempuri.org/">
         <getConfigSettingsResult><![CDATA[<root>
                        <status>
                            <id>0</id>
                            <message></message>
                        </status>

<drivers>
<drive id="tokenId 11">
  <shared-secret>Shared 11</shared-secret>
  <encoding>false</encoding>
  <compression />
</drive>
<drive id="tokenId 2 ">
  <shared-secret>Shared 2  </shared-secret>
  <encoding>false</encoding>
  <compression>false</compression>
</drive>
</drivers>
</root>]]></getConfigSettingsResult>
      </getConfigSettingsResponse>
   </s:Body>
</s:Envelope>

现在在一个 Java 类中,我执行以下操作:

XPath xPath;                        // An utility class for performing XPath calls on JDOM nodes

Element objectElement;              // An XML element
                        //xPath = XPath.newInstance("s:Envelope/s:Body/getVersionResponse/getVersionResult");

            try {
                // XPath selection:
                xPath = XPath.newInstance("s:Envelope/s:Body");
                xPath.addNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
                objectElement = (Element) xPath.selectSingleNode(documentXML);

                if (objectElement != null) {
                    result = objectElement.getValue();
                    System.out.println("RESULT:");
                    System.out.println(result);
                }
            } catch (JDOMException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

打印结果变量的内容的结果是这个输出:

RESULT:
<root>
                        <status>
                            <id>0</id>
                            <message></message>
                        </status>

<drivers>
<drive id="tokenId 11">
  <shared-secret>Shared 11</shared-secret>
  <encoding>false</encoding>
  <compression />
</drive>
<drive id="tokenId 2 ">
  <shared-secret>Shared 2  </shared-secret>
  <encoding>false</encoding>
  <compression>false</compression>
</drive>
</drivers>
</root>

现在我的问题是我只想访问0标签的内容,所以我希望(在这种情况下)我的结果变量必须包含0值。

但我不能,我尝试使用以下命令更改以前的 XPath 选择:

xPath = XPath.newInstance("s:Envelope/s:Body/s:status/s:id");

但是这样做我得到我的objectElementnull

为什么?我错过了什么?我该怎么做才能使 mu 结果变量包含id标签的内容?

肿瘤坏死因子

安德烈亚

4

2 回答 2

3

“CDATA”部分中的“根”节点。整个部分以文本形式插入,您无法通过 xPath 搜索它。您可以从“objectElement.getValue()”获取文本,像新的 XML 一样解析它,然后使用新的 xPath 获取标记“id”值。您也可以使用正则表达式在“objectElement.getValue()”中搜索标签“id”值。

于 2013-11-21T15:24:02.427 回答
0

实际上,您应该在 JDOM 2.x 中使用新的 XPathAPI,并考虑到 pasha701 的答案,您的代码应该看起来更像:

Namespace soap = Namespace.getNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
Namespace tempuri = Namespace.getNamespace("turi", ""http://tempuri.org/");
XPathExpression<Element> xpath = XPathFactory.instance().compile(
       "s:Envelope/s:Body/turi:getConfigSettingsResponse/turi:getConfigSettingsResult",
       Filters.element(), null, soap, tempuri);
Element result = xpath.evaluateFirst(documentXML);
String resultxml = result.getValue();
Document resultdoc = new SAXBuilder().build(new StringReader(resultxml));
Element id = resultdoc.getRootElement().getChild("status").getChild("id");
于 2013-11-21T15:47:26.310 回答