使用许多子节点将XML解析为UITableView

时间:2012-11-24 21:31:21

标签: iphone objective-c ios xml cocoa-touch

我有一个xml文件,如下所示:

<result>
<trip duration="03:30">
<takeoff date="2010-06-19" time="18:40" city="Moscow"/>
<landing date="2010-06-19" time="20:10" city="Novgorod"/>
<flight carrier="Rossiya" number="8395" eq="320"/>
<price>13429.00</price>
</trip>
<trip duration="03:40">
<takeoff date="2010-06-19" time="09:20" city="Omsk"/>
<landing date="2010-06-19" time="11:15" city="Paris"/>
<flight carrier="AirFrance" number="1145" eq="320"/>
<price>13229.00</price>
</trip>
<trip duration="03:50">
<takeoff date="2010-06-19" time="07:20" city="Omsk"/>
<landing date="2010-06-19" time="14:15" city="Barcelona"/>
<flight carrier="AirFrance" number="1100" eq="320"/>
<price>13329.00</price>
</trip>
</result>

我想获取所有参数并将它们放在3个UITableViewCells中。如您所见,有3个航班及其相关数据。 我一直在尝试在TableView中解析它,但我只是设法得到<price>的东西。 您将如何处理在XML文件中解析复杂结构化参数?我的意思是我如何得到takeoff date参数等等等? 据我所知,你不能像我这样使用我正在使用的策略:

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
        if ([elementname isEqualToString:@"price"]) 
        {
            currentTweet.dateCreated = currentNodeContent;
        }

任何帮助都将受到高度赞赏。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您还必须管理didStartElement。这是一个样本元代码可能对你有好处:

-(void)parser:(NSXMLParser*)parser didStartElement:(NSString *)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName attributes:(NSDictionary*)attributeDict {
    if([elementName isEqualToString:@"trip"]) {
       currentTweet.tripDuration = [attributeDict objectForKey:@"duration"];
    } else if ([elementName isEqualToString:@"takeoff"]) {
       currentTweet.takeoffDate = [attributeDict objectForKey:@"date"];
       currentTweet.takeoffTime = [attributeDict objectForKey:@"time"];
       currentTweet.takeoffCity = [attributeDict objectForKey:@"city"];
    } else if ([elementName isEqualToString:@"landing"]) {
       ...............
    } else if ...........
}

答案 1 :(得分:1)

这是我经常用来从XML文件构建字典的代码,该文件遵循一些定义良好的重复元素(在本例中为"trip")的基本模型,以及其中的一系列数据元素,其中一些我正在阅读标签的属性(在这种情况下,"takeoff""landing""flight"),其他我正在读取开始和结束之间的数据标签(在这种情况下,只是"price")。

我有以下ivars:

@interface XmlParserViewController () <NSXMLParserDelegate>
{
    NSMutableArray *trips;
    NSMutableDictionary *currentTrip;
    NSMutableString *currentElement;
}
@end

然后代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];

    trips = [[NSMutableArray alloc] init];

    // I'm getting my xml from my bundle. You get it however you're currently getting it.

    NSString *filename = [[NSBundle mainBundle] pathForResource:@"results" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:filename];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
}

#pragma mark - NSXMLParserDelegate methods

#define kRowElementTag @"trip"

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    NSArray *attributeElementNames = @[@"takeoff", @"landing", @"flight"];
    NSArray *foundCharacterElementNames = @[@"price"];

    if ([elementName isEqualToString:kRowElementTag])
    {
        currentTrip = [[NSMutableDictionary alloc] init];
        [trips addObject:currentTrip];
        if (attributeDict)
            [currentTrip setObject:attributeDict forKey:elementName];
    }
    else if (currentTrip)
    {
        if ([attributeElementNames containsObject:elementName])
        {
            if (attributeDict)
                [currentTrip setObject:attributeDict forKey:elementName];
        }
        else if ([foundCharacterElementNames containsObject:elementName] && currentElement == nil)
        {
            // you can change this to just grab a few fields ... add whatever fields you want to this

            currentElement = [[NSMutableString alloc] init];
            [currentTrip setObject:currentElement forKey:elementName];
        }
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:kRowElementTag])
    {
        currentTrip = nil;
    }
    else if (currentElement)
    {
        currentElement = nil;
    }
}

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

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"%s error=%@", __FUNCTION__, parseError);

    // we should handle the error here
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"%s trips=%@", __FUNCTION__, trips);

    // generally I immediately kick off the reload of the table, but maybe 
    // you want to grok the trips dictionary first.
    //        
    // [self.tableView reloadData];
}

你可以猜到,我正试图最终得到我们用来解析JSON文件的那种嵌套数组/字典结构。显然,我不喜欢我必须在我的代码中预先识别XML文件的一些结构这一事实(外部数组具有"trip"标签,"takeoff",{ {1}}和"landing"都有属性,但“价格”“没有”,等等。但这比我第一次尝试XML解析硬件编码值更好一点。叹息。