通过iOS Objective-C 2.0解析Google Search API XML结果

时间:2011-05-25 01:36:40

标签: objective-c xml ios nsxmlparser google-api

以下是我的Google API请求的XML结果:

<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>

以下是解析XML的NSXMLParser代码:

    @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 个答案:

答案 0 :(得分:1)

我认为itemImageURLArray应该包含网址字符串,而itemImagesArray包含网址指向的实际图片。

永远不会处理s:image元素,因为该代码位于if的{​​{1}}块内。为s:images调用didStartElement时,该代码永远不会运行。将其移至s:image的{​​{1}}之外。

此外,在处理if的代码中,您可能希望将url字符串添加到s:images数组中。在设置s:image的行之后,请尝试添加:

itemImageURLArray

至于将图像本身下载到thisLink,我会在xml解析后作为单独的步骤进行。您可以遍历[itemImageURLArray addObject:thisLink]; 并异步下载图像。


顺便说一句,这一行:

itemImagesArray

应该是:

itemImagesArray