2012-02-01 183 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