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

问题描述

|| 以下是我的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解析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
指针中?     

解决方法

我假设“ 6”应该包含网址字符串,而“ 7”应该是网址指向的实际图片。 never8ѭ元素不会被处理,因为该代码位于
s:images
if
块内。当为
s:image
调用
didStartElement
时,该代码将永远不会运行。将其移至
if
之外,移至
s:images
。 另外,在处理
s:image
的代码中,您可能希望将URL字符串添加到
itemImageURLArray
数组中。在设置
thisLink
的行之后,尝试添加:
[itemImageURLArray addObject:thisLink];
至于将图像本身下载到“ 7”中,我将在xml解析之后将其作为一个单独的步骤进行。您可以循环浏览ѭ7并异步下载图像。 顺便说一句,这行:
if (!\"s:product\")
应该可能是:
if (!productScanned)
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...