用于过滤 SOAP 消息的 JXPath

发布于 2024-12-11 06:46:01 字数 1364 浏览 0 评论 0原文

我正在尝试从 SOAP 响应中提取节点 GoodEmail 的值。似乎无论我尝试什么,我总是得到 org.apache.commons.jxpath.JXPathNotFoundException。

SOAP 响应:

<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>

Java 代码:

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");

在我看来,错误代码表明 xpath 过滤器是错误的。我尝试了几种不同的方法,但都无法做到正确。我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素衣风尘叹 2024-12-18 06:46:01

这是 XPath 中最多的常见问题。搜索“xpath默认命名空间”,您会找到很多好的答案。

简单的解释是,在 XPath 中,任何无前缀的名称都被视为属于“无命名空间”。因此,如果我们想要指定属于某个命名空间(包括默认命名空间)的元素的名称,这些名称必须带有前缀,并且指定的前缀必须关联起来(使用特定的 XPath 引擎 API)以及特定节点所在的命名空间的命名空间-uri。

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

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

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

This is the most FAQ in XPath. Search for "xpath default namespace" and you'll find many good answers.

A brief explanation is that in XPath any unprefixed name is considered to belong to "no namespace". Therefore, if we want to specify the names of elements that belong to some namespace (including a default namesopace), these names must be prefixed and the prefix specified must be associated (using the particular XPath engine API) withthe namespace-uri of the namespace, in which the particular node resides.

Therefore, the expression could be something like this:

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

where the prefix "x" is associated (by "registering" this namespace association) with the namespace "http://ws.cdyne.com/"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文