NSDictionaryのNSArrayへのXML

私はこの行に沿ってXMLファイルを持っています:



<title>123 Main St</title>
123 Main St, Georgetown Washingon D.C.
http://www.photo.com/images/1.jpg
http://www.photo.com/images/2.jpg
http://www.photo.com/images/3.jpg 
http://www.photo.com/images/4.jpg



<title>1234 Main St</title>
1234 Main St, Georgetown Washingon D.C.
http://www.photo.com/images/1.jpg
http://www.photo.com/images/2.jpg
http://www.photo.com/images/3.jpg 
http://www.photo.com/images/4.jpg

and need a way to populate a NSArray with NSDictonarys of the info between the tags.

I have found Parse xml item "Category" into an NSArray with NSDictionary but cannot seem to make it work. If anyone has any ideas how to do this or a better way to import data into my app and help would greatly appreciated.

1
追加された 編集された
ビュー: 1

1 答え

MacOSのみ:

ここにコードです:

NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"xml" ofType:@"xml"];
NSString *xml = [NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:nil];
NSXMLDocument *xmlDocument = [[[NSXMLDocument alloc] initWithXMLString:xml options:0 error:nil] autorelease];
NSMutableArray *array = [NSMutableArray array];

for (NSXMLElement *node in [xmlDocument.rootElement nodesForXPath:@"//house" error:nil])
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSMutableArray *photos = [NSMutableArray array];

    for (NSXMLElement *subNode in node.children)
{
        if ([subNode.name isEqualToString:@"photo"])
            [photos addObject:subNode.stringValue];
        else 
            [dict setObject:subNode.stringValue forKey:subNode.name];
    }

    [dict setObject:photos forKey:@"photos"];
    [array addObject:dict];
}

NSLog(@"%@", array);

そして、ここに出力されます:

(
{
    address = "123 Main St, Georgetown Washingon D.C.";
    photos =         (
        "http://www.photo.com/images/1.jpg",
        "http://www.photo.com/images/2.jpg",
        "http://www.photo.com/images/3.jpg",
        "http://www.photo.com/images/4.jpg"
    );
    title = "123 Main St";
},
    {
    address = "1234 Main St, Georgetown Washingon D.C.";
    photos =         (
        "http://www.photo.com/images/1.jpg",
        "http://www.photo.com/images/2.jpg",
        "http://www.photo.com/images/3.jpg",
        "http://www.photo.com/images/4.jpg"
    );
    title = "1234 Main St";
}
}
1
追加された
注:NSXMLDocumentはiOSでは使用できません。
追加された 著者 Mustafa,