为什么我能';是否在Java方法中使用XPath获取标记的内容?

为什么我能';是否在Java方法中使用XPath获取标记的内容?,java,xml,xpath,jdom,jdom-2,Java,Xml,Xpath,Jdom,Jdom 2,我对XPath非常陌生,我有以下问题: 我有一个Java方法,它从Web服务接收数据,这些数据位于XML文档中,因此我必须使用XPath在XML结果文档中获取特定值 我特别注意到,这是我的web服务(web服务响应)提供的整个XML输出: 打印结果变量的内容的结果是该输出: RESULT: <root> <status> <id>0</id>

我对XPath非常陌生,我有以下问题:

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

我特别注意到,这是我的web服务(web服务响应)提供的整个XML输出:

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

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>
但通过这种方式,我得到我的objectElementnull

为什么??我错过了什么?要获得包含id标记内容的mu结果变量,我必须做些什么

Tnx


Andrea

您的“根”节点位于“CDATA”部分。整个部分解释为文本,不能通过xPath进行搜索。您可以从“objectElement.getValue()”获取文本,像解析新XML一样解析它,然后使用新的xPath获取标记“id”值。您还可以使用正则表达式搜索“objectElement.getValue()”中的标记“id”值。

实际上,您应该在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");
Namespace soap=Namespace.getNamespace(“s”)http://schemas.xmlsoap.org/soap/envelope/");
Namespace tempuri=Namespace.getNamespace(“turi”)http://tempuri.org/");
XPathExpression xpath=XPathFactory.instance().compile(
“s:Envelope/s:Body/turi:getConfigSettingsResponse/turi:getConfigSettingsResult”,
Filters.element(),null,soap,tempuri);
元素结果=xpath.evaluateFirst(documentXML);
字符串resultxml=result.getValue();
Document resultdoc=new SAXBuilder().build(new StringReader(resultxml));
元素id=resultdoc.getRootElement().getChild(“状态”).getChild(“id”);

mmm您对我说我的“根”节点位于“CDATA”部分是什么意思?我该怎么做才能“像解析新的XML一样解析它”?您可以在XML中看到“”。有关CDATA的更多详细信息,请参见此处:您可以从DOM的角度以解析整个“的方式解析“objectElement.getValue()”中的文本,里面的整个块只是一块文本。您可以获取该文本块并将其解析为XML,然后就可以在其上运行XPath了。@hcayless好的,但我该怎么做才能获取该文本块并将其解析为XML?您使用的是JDOM 1.x还是2.x?
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>
xPath = XPath.newInstance("s:Envelope/s:Body/s:status/s:id");
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");