2012-02-01 152 views
3

我正在深化发展iOS应用程序和我解析我的XML与gdataxml,但我做错了,解析XML我的NSLog是空与GDATA XML命名空间

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(@ “周一阵列%@”,tempArray);

我的数组为null,

我的XML是这样的:

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

我是苏尔,我problème与命名空间,但我不怎么办呢?

谢谢您的回答

+0

任何想法,为什么这不起作用? – samir 2012-02-01 17:55:59

+0

你已经解决了这个问题吗?有一些我不了解GDataXML的东西,所以这真的很有趣。 – yoninja 2012-02-22 16:46:44

+0

您好萨米尔,我不能够得到nodesForXPath的元素,它返回类似的XPath错误的错误:未定义命名空间前缀 xmlXPathEval:评估失败 我需要你的帮助......从过去三天呆在这里,我不想给使用命名空间参数,所以我可以做到这一点? – Apple 2013-06-21 10:06:38

回答

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]; 
+0

坦克,我已经测试它和它的正常工作 – samir 2012-02-23 20:35:45

+0

你好yoninja,我有错误,如XPath的错误:未定义命名空间前缀xmlXPathEval:评估失败。当我使用nodesForXPath。 – Apple 2013-06-21 09:54:56

+0

嗨@TapanNathvani,你能告诉我你是如何做到的?或者,也许你可以发表一个关于这个问题。谢谢! – yoninja 2013-06-28 05:30:23