用于过滤 SOAP 消息的 JXPath
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 XPath 中最多的常见问题。搜索
“xpath默认命名空间”
,您会找到很多好的答案。简单的解释是,在 XPath 中,任何无前缀的名称都被视为属于“无命名空间”。因此,如果我们想要指定属于某个命名空间(包括默认命名空间)的元素的名称,这些名称必须带有前缀,并且指定的前缀必须关联起来(使用特定的 XPath 引擎 API)以及特定节点所在的命名空间的命名空间-uri。
因此,表达式可能是这样的:
其中前缀
“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:
where the prefix
"x"
is associated (by "registering" this namespace association) with the namespace"http://ws.cdyne.com/"