2011-05-25 45 views
0

下面是我的谷歌API請求XML結果:解析谷歌通過iOS版搜索API XML結果的Objective-C 2.0

<entry gd:kind="shopping#product"> 
<author> 
<name>Bloomingdale&apos;s</name> 
</author> 
<title>7 For All Mankind &quot;Ginger&quot; Wide Leg Jeans in Lightweight Mercer Wash</title> 
<s:product> 
<s:author> 
<s:name>Bloomingdale&apos;s</s:name> 
<s:accountId>8020</s:accountId> 
</s:author> 
<s:title>7 For All Mankind &quot;Ginger&quot; Wide Leg Jeans in Lightweight Mercer Wash</s:title> 
<s:description>7 for all Mankind &quot;Ginger&quot; jeans in lightweight mercer wash. A wide-leg fit.</s:description> 
<s:brand>7 For All Mankind</s:brand> 
<s:gtin>12345680753539</s:gtin> 
<s:images> 
<s:image link="http://images.bloomingdales.com/is/image/BLM/products/2/optimized/1144762_fpx.tif?wid=287&amp;qlt=90"/> 
<s:image link="http://1.0.0.0/"/> 
<s:image link="http://0.0.0.5/"/> 
</s:images> 
</s:product> 
</entry> 

這裏是我的NSXMLParser代碼來解析XML:

@implementation ItemViewController

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName 
attributes:(NSDictionary *)attributeDict 
{ 
if ([elementName isEqual:@"s:product"]) { 
    NSLog(@"found Product!"); 
    if (!"s:product") 
     productScanned = [[NSMutableArray alloc] init]; 
    return; 
} 

if ([elementName isEqual:@"s:gtin"]) { 
    // set ItemNumber as the GTIN of the productScanned  
    itemNumber = [[NSMutableString alloc] init]; 
} 

if ([elementName isEqual:@"s:title"]) { 
    // set ItemDesc as the item description of the productScanned 
    itemDesc = [[NSMutableString alloc] init]; 
} 

if ([elementName isEqual:@"s:brand"]) { 
    // set ItemBrand as the brand description of the productScanned 
    itemBrand = [[NSMutableString alloc] init]; 
} 

if ([elementName isEqual:@"s:name"]) { 
    // set ItemStore as the store location of the productScanned 
    itemStore = [[NSMutableString alloc] init]; 
} 

// Create array of Image Tags 
if ([elementName isEqual:@"s:images"]) { 
    NSLog(@"found Images!"); 
    if (!itemImageURLArray) 
     itemImageURLArray = [[NSMutableArray alloc] init]; 
    return; 

    if ([elementName isEqualToString:@"s:image"]) { 
     // itemImagesArray is an NSMutableArray instance variable 
     if (!itemImagesArray) 
      itemImagesArray = [[NSMutableArray alloc] init]; 
     NSString *thisLink = [attributeDict objectForKey:@"link"]; 
     if (thisLink) 
      // do something 
     return; 
    } 
} 
itemImageURL = [itemImagesArray objectAtIndex:0]; 
} 

爲什麼我的成績回報所有元素信息除了<s:images><s:image>埃爾對此語句?

更好。幫我理解爲什麼我的數組解析<s:images>沒有將這些元素加載到我的*itemImageURL指針中?

+1

什麼行'如果( 「S:影像」!)'是什麼意思? – Anna 2011-05-25 03:08:30

+0

Typo in code ...已被'if(!itemImageURLArray)'替換爲' Thx for捕捉那個,Anna – brussels0828 2011-05-25 03:17:34

回答

1

我假設itemImageURLArray應該包含網址字符串和itemImagesArray網址指向的實際圖片。

s:image元素永遠不會被處理,因爲該代碼位於s:imagesif塊內。當didStartElement被調用爲s:image時,該代碼從不運行。將它移動到if以外s:images

此外,在處理s:image的代碼中,您可能希望將url字符串添加到itemImageURLArray數組。那臺thisLink行後,試着加入:

[itemImageURLArray addObject:thisLink]; 

至於下載圖像本身爲itemImagesArray,我會做它作爲XML解析後的單獨步驟。您可以循環訪問itemImagesArray並以異步方式下載圖像。


順便說一句,這條線:

if (!"s:product") 

也許應該是:

if (!productScanned)