Objective C 中的 XPath 和 GDataXMLNode

发布于 2024-12-15 19:13:20 字数 1025 浏览 3 评论 0原文

我正在制作一个 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 技术交流群。

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

发布评论

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

评论(1

罪#恶を代价 2024-12-22 19:13:20

这是常见问题解答(搜索 XPath 和默认命名空间)。

简而言之,当计算 XPath 表达式时,任何无前缀的名称都被假定为“无命名空间”。但 MakeRequestResponse 不在“无命名空间”中,因此未被选中。

解决方案

注册 "http://tempuri.org/" 命名空间并为其关联一个前缀(例如 "x:") ,然后使用:

soap:Body/x:MakeRequestResponse

或者,否则你可以有这样的表达式:

soap:Body/*[name() = 'MakeRequestResponse']

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:

soap:Body/x:MakeRequestResponse

Or, otherwise you can have an expression like this:

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