1

我尝试加载属性元素。在这篇文章中有很多代码来显示我所拥有的并(希望)清除所有问题。

解析器.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

解析器

#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];
4

1 回答 1

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 方法调用以获取图像 url 字符串的方法:

-(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;
}

获得图像的 url 后,您可以使用它来下载它们并在您的应用程序中显示您想要的任何位置

于 2013-04-23T10:38:40.347 回答