gpt4 book ai didi

iphone - 将具有大量子节点的 XML 解析为 UITableView

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:50 24 4
gpt4 key购买 nike

我有一个如下所示的 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;
}

任何帮助将不胜感激。提前致谢。

最佳答案

这是我经常用来从 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", "landing", and "flight" 有属性但"price"`没有), etc.但这有点比我第一次尝试在 XML 中到处解析硬编码值要好。叹息。

关于iphone - 将具有大量子节点的 XML 解析为 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13545811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com