1

I'm trying to extract the value of the node GoodEmail from a SOAP response. It seems that no matter what I try, I always get a org.apache.commons.jxpath.JXPathNotFoundException.

SOAP response:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
      <VerifyEmailResponse xmlns="http://ws.cdyne.com/">
         <VerifyEmailResult>
            <ResponseText>Invalid Email Address</ResponseText>
            <ResponseCode>0</ResponseCode>
            <LastMailServer/>
            <GoodEmail>false</GoodEmail>
         </VerifyEmailResult>
      </VerifyEmailResponse>
   </soap:Body>
</soap:Envelope>

The Java code:

String payload = message.getPayloadAsString(); //The SOAP response
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db =  dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(payload));

Document doc = db.parse(is);
JXPathContext context = JXPathContext.newContext(doc);
String jxpathReply = (String) context.getValue("//soap:Envelope/soap:Body/VerifyEmailResponse/VerifyEmailResult/GoodEmail");

To me it seems that the error code suggest that it is the xpath filter that is wrong. I've tried a few different, but can't get it right. What am I doing wrong?

4

1 回答 1

0

这是 XPath 中最常见的问题。搜索"xpath default namespace",你会找到很多好的答案。

一个简短的解释是,在 XPath 中,任何无前缀的名称都被认为属于“无命名空间”。因此,如果我们要指定属于某个命名空间(包括默认命名空间)的元素的名称,这些名称必须加上前缀,并且指定的前缀必须与命名空间的命名空间 uri 相关联(使用特定的 XPath 引擎 API) , 特定节点所在的位置。

因此,表达式可能是这样的

/soap:Envelope/soap:Body/x:VerifyEmailResponse/x:VerifyEmailResult/x:GoodEmail

前缀与命名空间相关联的位置"x"(通过“注册”此命名空间关联)"http://ws.cdyne.com/"

于 2011-10-20T13:00:35.640 回答