属性付きの要素を読み込もうとしています。この投稿には、私が持っているものを示し、(できれば) すべての質問をクリアするための多くのコードがあります。
パーサー.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
パーサー.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];