解析谷歌通过iOS版搜索API XML结果的Objective-C 2.0
问题描述:
下面是我的谷歌API请求XML结果:解析谷歌通过iOS版搜索API XML结果的Objective-C 2.0
<entry gd:kind="shopping#product">
<author>
<name>Bloomingdale's</name>
</author>
<title>7 For All Mankind "Ginger" Wide Leg Jeans in Lightweight Mercer Wash</title>
<s:product>
<s:author>
<s:name>Bloomingdale's</s:name>
<s:accountId>8020</s:accountId>
</s:author>
<s:title>7 For All Mankind "Ginger" Wide Leg Jeans in Lightweight Mercer Wash</s:title>
<s:description>7 for all Mankind "Ginger" 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&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
指针中?
答
我假设itemImageURLArray
应该包含网址字符串和itemImagesArray
网址指向的实际图片。
s:image
元素永远不会被处理,因为该代码位于s:images
的if
块内。当didStartElement
被调用为s:image
时,该代码从不运行。将它移动到if
以外s:images
。
此外,在处理s:image
的代码中,您可能希望将url字符串添加到itemImageURLArray
数组。那台thisLink
行后,试着加入:
[itemImageURLArray addObject:thisLink];
至于下载图像本身为itemImagesArray
,我会做它作为XML解析后的单独步骤。您可以循环访问itemImagesArray
并以异步方式下载图像。
顺便说一句,这条线:
if (!"s:product")
也许应该是:
if (!productScanned)
什么行'如果( “S:影像”!)'是什么意思? – Anna 2011-05-25 03:08:30
Typo in code ...已被'if(!itemImageURLArray)'替换为' Thx for捕捉那个,Anna – brussels0828 2011-05-25 03:17:34