我尝试加载属性元素。在这篇文章中有很多代码来显示我所拥有的并(希望)清除所有问题。
解析器.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];