使用gdata xml解析带有名称空间的xml

时间:2012-02-01 17:08:01

标签: objective-c ios gdataxml

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

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 array%@”,tempArray);

我的数组为空,

我的xml是这样的:

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

我觉得我的问题与命名空间有关,但我不知道怎么做?

感谢您的回答

1 个答案:

答案 0 :(得分:8)

使用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];
相关问题