【问题标题】:parse xml with namespaces with gdata xml使用 gdata xml 解析带有名称空间的 xml
【发布时间】:2012-02-01 17:08:00
【问题描述】:

我正在开发一个 ios 应用程序,我正在使用 gdataxml 解析我的 xml,但我做错了,我的 nslog 为空

NSError *error = nil;
GDataXMLDocument *xmlResult = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
if (error) {
    NSLog(@"%@",error);
}

NSLog(@"%@",xmlResult.rootElement); 我的根元素是完美的,错误是 tempArray

NSArray *tempArray = [xmlResult nodesForXPath:@"//message/error/value" error:&error];

NSLog(@"mon 数组 %@",tempArray);

我的数组为空,

我的 xml 是这样的:

<message xmlns="http://.....Api" xmlns:i="http://www.w3.org/....">
<error i:nil="true"/>
<value>

我知道我的问题在于命名空间,但我不知道该怎么做?

感谢您的回答

【问题讨论】:

  • 知道为什么这不起作用吗?
  • 您已经解决了这里的问题吗?关于 GDataXML 有很多我不知道的事情,所以这真的很有趣。
  • 你好 samir,我无法从 nodesForXPath 获取元素,它返回类似 XPath 错误的错误:未定义的命名空间前缀 xmlXPathEval:评估失败,我需要你的帮助......从过去 3 天开始卡在这里而且我不想使用命名空间参数,所以我可以这样做吗?

标签: objective-c ios gdataxml


【解决方案1】:

在使用 GDataXMLNode 进行一些测试后,这是我的答案:

NSArray *tempArray = [xmlResult nodesForXPath:@"//_def_ns:message/_def_ns:error/_def_ns:value" error:&error];

您可以在 GDataXMLNode.h 中看到这条注释:

// This implementation of nodesForXPath registers namespaces only from the
// document's root node.  _def_ns may be used as a prefix for the default
// namespace, though there's no guarantee that the default namespace will
// be consistenly the same namespace in server responses.

它表明您实际上可以使用 _def_ns 作为您的命名空间。但是,您也可以设置自己的命名空间,以防文档中存在其他命名空间。

NSDictionary *myNS = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"http://.....Api", @"ns1",
                      @"http://.....Other_Api", @"ns2", nil];
NSArray *tempArray = [xmlResult nodesForXPath:@"//ns1:message/ns1:error/ns1:value" namespaces:myNS error:&error];

【讨论】:

  • 你好 yoninja,我收到类似 XPath 错误的错误:未定义的命名空间前缀 xmlXPathEval:评估失败。当我使用nodesForXPath.
  • 嗨@TapanNathvani,你能告诉我你是怎么做到的吗?或者,也许您可​​以发布关于此的问题。谢谢!
  • 你好 yonija,.. 我解决了这个问题。感谢您的回复。这是我的问题链接。 stackoverflow.com/questions/17232712/…感谢您的回复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-09
  • 2010-09-12
  • 2014-07-28
  • 2018-12-30
相关资源
最近更新 更多