Objective C 中的 XPath 和 GDataXMLNode
我正在制作一个 iOS 应用程序,其中使用 GDataXML 库在 Objective C 中进行 xml 解析。
我有以下 xml(作为肥皂响应得到):
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<MakeRequestResponse xmlns="http://tempuri.org/">
<MakeRequestResult>SOMERANDOMDATAHERE012912033905345346</MakeRequestResult>
</MakeRequestResponse>
</soap:Body>
</soap:Envelope>
我遇到的问题是:
当我为给定这样的 xml,我得到 MakeRequestResponse 节点:
NSArray *listOfNodes = [[responseDocument rootElement] nodesForXPath:@"soap:Body/*" error:&error];
但是,当我编写实际节点名称时,我无法获取此节点或下面的任何子节点:
NSArray *listOfNodes = [[responseDocument rootElement] nodesForXPath:@"soap:Body/MakeRequestResponse" error:&error];
我不确定这里有什么问题。这可能是与命名空间相关的问题吗?
I am making an iOS app where I'm using the GDataXML library for xml parsing in Objective C.
I have the following xml (that I get as an soap response):
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<MakeRequestResponse xmlns="http://tempuri.org/">
<MakeRequestResult>SOMERANDOMDATAHERE012912033905345346</MakeRequestResult>
</MakeRequestResponse>
</soap:Body>
</soap:Envelope>
The problem I have is:
when I write the xpath expression for the given xml like this I get the MakeRequestResponse node:
NSArray *listOfNodes = [[responseDocument rootElement] nodesForXPath:@"soap:Body/*" error:&error];
However I'm unable to get this node or any children below when I write the actual node name:
NSArray *listOfNodes = [[responseDocument rootElement] nodesForXPath:@"soap:Body/MakeRequestResponse" error:&error];
I'm not sure what is the issue here. Could it be an namespace related issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是常见问题解答(搜索 XPath 和默认命名空间)。
简而言之,当计算 XPath 表达式时,任何无前缀的名称都被假定为“无命名空间”。但
MakeRequestResponse
不在“无命名空间”中,因此未被选中。解决方案:
注册
"http://tempuri.org/"
命名空间并为其关联一个前缀(例如"x:"
) ,然后使用:或者,否则你可以有这样的表达式:
This is a FAQ (search for XPath and default namespace).
The short answer is that when an XPath expression is evaluated, any unprefixed names are assumed to be in "no namespace". But
MakeRequestResponse
is not in "no namespace" and thus isn't selected.Solution:
Either register the
"http://tempuri.org/"
namespace and associate a prefix (say"x:"
) to it, then use:Or, otherwise you can have an expression like this: