1
這是我昨天發佈的問題的後續操作。Objective-C nodesForXPath返回所有結果
鑑於下面發佈的汽車XML示例,我運行了一個xpath查詢,循環遍歷結果並使用子元素調用「quickquery - getString」。我期望在循環的每次迭代中,我都會在getString函數中獲得一個單獨的元素,但是我沒有。相反,在getString函數內部的nodesForXPath調用將返回所有4個汽車名稱,而不僅僅是屬於該子元素的名稱。
NSArray *listings = [response nodesForXPath:@"//car" error:&error];
//4 car elements found
if(listings.count > 0)
{
for (GDataXMLElement *listingElement in listings)
{
//printing listingElements description at this point reveales only one car
[QuickQuery getString:listingElement fromXPath:@"//name"];
}
}
//this is defined in QuickQuery class
+(NSString *) getString:(GDataXMLElement *)element fromXPath:(NSString *)xPath
{
NSError *error;
//Query the name element for the spcific car element passed in
NSArray *result = [element nodesForXPath:xPath error:&error]; /// ***THIS CALL
//result.count is 4 at this stage ("BMW", "VW" ... etc)
//out of the 4 calls made to this method, I would expect each value to come up once
//but each time all 4 values are found.
if(result.count > 0)
{
GDataXMLElement *singleValue = (GDataXMLElement *) [result objectAtIndex:0];
return singleValue.stringValue;
}
return nil;
}
<cars>
<car>
<name>VW</name>
</car>
<car>
<name>BMW</name>
</car>
<car>
<name>Mazda</name>
</car>
<car>
<name>Nissan</name>
</car>
</cars>
這個問題已經發布之前,這只是一個更清晰的例子和代碼。標題也更具體。
OMW ...我想你實際上已經釘它。今晚我會回家測試,讓你知道它是怎麼回事。謝謝。 – Craigt
感謝您的答案..我將查詢更改爲./name,現在它完美工作。 – Craigt