0

我想把这个XML:

<tennis>
<date>01-01-2013
<tournament>
<location>
<city>London</city>
<venue>Wimbledon</venue>
</location>
<match>
<player>Andy Murray</player>
<player>Roger Federer</player>
</match>
<match>
<player>Rafa Nadal</player>
<player>Andy Roddick</player>
</match>
</tournament>

<date>02-01-2013
<tournament>
<location>...

进入显示日期的表格视图,选择该表格后,将导航到分组的表格视图,其中 Venue 作为部分标题,并在表格行中列出匹配项。

我的问题是最好的方法是什么。目前我正在使用 NSXMLParser 来..

  • 创建一个字典,每个 Date 都是 Venues 数组的键。
  • 创建 Match 对象,并将每个 Venue 的 Match 数组存储在字典中。

then my first tableview is just displays the keys from the Date dictionary, and when a row is selected it 'passes' the associated array of Venues to the next tableview class..

然后从第二个字典中选择 Match 对象,使用传入的 Venues 数组作为查找的键。

抱歉,如果这令人困惑,当我写出来时是对我的。

  • 这是构建数组和字典、设置标志以保持状态等的 NSXMLParser 方法,是像我这样的 XML 推荐的方法吗?

  • 我应该使用 DOM 解析器吗?

提前致谢。

4

2 回答 2

0

你可以试试这个 xml 解析器:

SMXML文档

它使 xml 变成漂亮的数组或字典供您使用

于 2012-10-29T14:20:59.813 回答
0

我用来XSXMLparser从网站解析一些 XML,然后通过以下方式在表中显示键:

[myDictionary allKeys]objectAtIndex:indexPath.row;

然后在选择时创建表的一个新实例并通过属性设置重要变量:

tableScreen *tableScreenNew = [[tableScreen alloc]init];
            tableScreenNew.currentLevel = 1;
            tableScreenNew.numberOfRows = counter;
            tableScreenNew.parsedData = parsedData;
            [self.navigationController pushViewController:tableScreenNew animated:YES];

这将使用我需要的数据推送新表,并且cellForRow:我只有一个 if:

if(currentLevel == 0)
{

}
else if(currentLevel == 1)
{

}
else
{
 NSLog(@"I Messed up");
}

并在 cellForRow 中为您的情况分配一个临时字典:

NSDictionary *tempDictionary = [NSDictionary dictionaryWithDictionary:[originalParsedData valueForKey:dateKeyNameFromLastTable]];

编辑:解析代码

NSData *myData = [xmlToParse dataUsingEncoding:NSUTF16StringEncoding];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:myData];
    [parser setDelegate:self];

 if([parser parse])
    {
         NSLog(@"Success");
         NSLog(@"****************************** Finished Dictionary ******************************");
         for(int i = 0;i < [parsedData count];i++)
         {
             NSLog(@"%d:%@",i,[parsedData objectAtIndex:i]);
         }
         NSLog(@"*********************************************************************************");
     }
    else
    {
         NSLog(@"Epic Fail");
         NSLog(@"%@",[parser parserError]);
    }




   - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict
{
    NSLog(@"Processing Element: %@", elementName);
    NSLog(@"Processing Attributes: %@", attributeDict);

    //Here you would start adding the key/Dicts to a NSMutableDictionary which you would use to populate tables

[myNSMutableDictionary addObject:attributeDict forKey:elementName];
}
于 2012-10-29T14:31:39.357 回答