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

发布于 2024-11-09 16:38:55 字数 2990 浏览 3 评论 0原文

以下是我的 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];
}

为什么我的结果返回除 元素之外的所有元素信息?

更好了。帮助我理解为什么我的用于解析 的数组没有将这些元素加载到我的 *itemImageURL 指针中?

Here are the XML results from my Google API request:

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

Here is my NSXMLParser code to parse the 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];
}

Why do my results return all element information except for the <s:images> and <s:image> elements?

Better yet. Help me understand why my array for parsing <s:images> isn't loading those elements into my *itemImageURL pointer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-11-16 16:38:55

我假设 itemImageURLArray 应该包含 url 字符串和 itemImagesArray url 指向的实际图像。

s:image 元素永远不会被处理,因为该代码位于 s:imagesif 块内。当为 s:image 调用 didStartElement 时,该代码永远不会运行。将其移至 s:imagesif 之外。

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

[itemImageURLArray addObject:thisLink];

至于将图像本身下载到 itemImagesArray 中,我会在 xml 解析之后将其作为单独的步骤来执行。您可以循环遍历 itemImagesArray 并异步下载图像。

顺便说一句,这一行:

if (!"s:product")

可能应该是:

if (!productScanned)

I assume itemImageURLArray is supposed to contain the url strings and itemImagesArray the actual images the urls point to.

The s:image elements are never processed because that code is inside the if block for s:images. When didStartElement is called for s:image, that code never runs. Move it outside the if for s:images.

Also, in the code that handles s:image, you probably want to add the url string to the itemImageURLArray array. After the line that sets thisLink, try adding:

[itemImageURLArray addObject:thisLink];

As for downloading the images themselves into itemImagesArray, I would do it as a separate step after the xml parsing. You can loop through the itemImagesArray and download the images asynchronously.

By the way, this line:

if (!"s:product")

should probably be:

if (!productScanned)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文