CDATA块内的NSXML分析器图像URL

时间:2013-04-23 09:54:38

标签: objective-c nsxmlparser

我尝试加载一个属性元素。在这篇文章中有很多代码可以显示我所拥有的内容并清楚(希望)所有问题。

Parser.h

#import <Foundation/Foundation.h>

@interface Parser : NSXMLParser

@property (nonatomic, strong) NSString *rowElementName; // this is the element name that identifies a new row of data in the XML
@property (nonatomic, strong) NSArray *attributeNames;  // this is the array of attributes we might want to retrieve for that element name
@property (nonatomic, strong) NSArray *elementNames;    // this is the list of sub element names for which we're retrieving values

@property (nonatomic, strong) NSMutableArray *items;    // after parsing, this is the array of parsed items
@end

Parser.m

#import "Parser.h"

@interface Parser () <NSXMLParserDelegate>

@property (nonatomic, strong) NSMutableDictionary *item;     // while parsing, this is the item currently being parsed
@property (nonatomic, strong) NSMutableString *elementValue; // this is the element within that item being parsed

@end

@implementation Parser

- (id)initWithContentsOfURL:(NSURL *)url{
    self = [super initWithContentsOfURL:url];
    if (self){
        self.delegate = self;
    }
    return self;
}

- (id)initWithData:(NSData *)data{
    self = [super initWithData:data];
    if (self){
        self.delegate = self;
    }
    return self;
}

- (id)initWithStream:(NSInputStream *)stream{
    self = [super initWithStream:stream];

    if (self){
        self.delegate = self;
    }
    return self;
}

#pragma mark - NSXMLParserDelegate methods

- (void)parserDidStartDocument:(NSXMLParser *)parser{
    self.items = [[NSMutableArray alloc] init];

    if (!self.rowElementName)
        NSLog(@"%s Warning: Failed to specify row identifier element name", __FUNCTION__);
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqualToString:self.rowElementName]){
        self.item  = [[NSMutableDictionary alloc] init];

        for (NSString *attributeName in self.attributeNames){
            id attributeValue = [attributeDict valueForKey:attributeName];
            if (attributeValue)
                [self.item setObject:attributeValue forKey:attributeName];
        }
    }
    else if ([self.elementNames containsObject:elementName]){
        self.elementValue = [[NSMutableString alloc] init];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if (self.elementValue){
        [self.elementValue appendString:string];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:self.rowElementName]){
        [self.items addObject:self.item];
        self.item = nil;
    }
    else if ([self.elementNames containsObject:elementName]){
        [self.item setValue:self.elementValue forKey:elementName];
        self.elementValue = nil;
    }
}

@end

XML结构:

<?xml version="1.0" encoding="UTF-8"?> <Document>
    <Placemark>
        <name><![CDATA[FooName]]></name>
        <Snippet><![CDATA[FooSniped]]></Snippet>
        <description><![CDATA[<img src="http://www.foosite.com/fooimage.jpg">FooDescription]]></description>
    </Placemark> </Document>

现在回答我真正的问题。我没有问题用通常的标签解析所有的信息,但我无法从描述中获取图像URL。我按常规方式做事:
parser.attributeNames = @[@"src"];
但它返回(null)所以我觉得有些不对劲。有什么想法吗?

修改

我正在使用这个简单的行:

Parser *parser = [[Parser alloc] initWithContentsOfURL:url];
parser.rowElementName = @"Placemark";
parser.elementNames = @[@"name", @"Snippet", @"description"];
parser.attributeNames = @[@"src"]; //return (null)
[parser parse];

1 个答案:

答案 0 :(得分:2)

您是否考虑过使用NSXMLParser类的CDATA委托方法:

首先,您需要使用“foundCData”委托方法。它将帮助您从正在解析的Web服务中获取CDATA块中的图像URL。

  - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock {


    NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];

    [elementValue appendString:someString];

    NSString *imageURLString = [self getFirstImageUrl:someString];
    NSURL *imageURL = [NSURL URLWithString:imageURLString];
}

以下是您从CDATA方法调用以获取图片网址字符串的方法:

-(NSString *)getFirstImageUrl: (NSString *) html {

    NSScanner *theScanner;
    NSString *imageURL = nil;

    theScanner = [NSScanner scannerWithString: html];

    // find start of tag
    [theScanner scanUpToString: @"<img" intoString: NULL];
    if ([theScanner isAtEnd] == NO) {

        [theScanner scanUpToString: @"src=\"" intoString: NULL];
        NSInteger newLoc2 = [theScanner scanLocation] + 5;
        [theScanner setScanLocation: newLoc2];

        // find end of tag
        [theScanner scanUpToString: @"\"" intoString: &imageURL];
    }

    return imageURL;
}

获得图片的网址后,您可以使用它来下载图片并在应用中显示您想要的位置