2011-06-28 10 views
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> 

この質問は前に投稿されていますが、これはほんの徹底的な例とコードです。タイトルもより具体的です。あなたが見ている何

答えて

4

は、クエリ「//名前」の正しい動作です。ドキュメントのルートではなく、現在のノードを基準にしたクエリを使用する必要があります。

便利なのXPathチュートリアルhttp://www.w3schools.com/xpath/

+0

OMWを見てみましょう...私はあなたが実際にそれを釘付けしていると思います。私は今夜​​、家に帰るときにテストして、それがどのようになったかを知らせます。ありがとうございました。答えのための – Craigt

+2

おかげで..私は./nameするクエリを変更し、それが今で完璧に動作します。 – Craigt

関連する問題