我真的很挣扎。我有一个XML feed:
<?xml version="1.0" encoding="ISO-8859-1"?>
<data>
<date>
<name>27/9/10</name>
<event>
<title>Event 1</title>
<more>Copy in here</more>
</event>
</date>
<date>
<name>04/10/10</name>
<event>
<title>Event 1</title>
<more>Copy in here</more>
</event>
<event>
<title>Second Event</title>
<more>Copy in here</more>
</event>
</date>
</data>
我可以设法用:
解析xml- (void)parseXMLFileAtURL:(NSString *)URL {
sections = [[NSMutableArray alloc] init];
items = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser parse];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser {
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:@"event"]) {
currentTitle = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
}
if ([elementName isEqualToString:@"date"]) {
item = [[NSMutableDictionary alloc] init];
currentSection = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"date"]) {
[item setObject:[NSNumber numberWithInt:itemsCount] forKey:@"itemsCount"];
[item setObject:currentSection forKey:@"name"];
[items addObject:[item copy]];
itemsCount = 0;
}
if ([elementName isEqualToString:@"event"]) {
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentSummary forKey:@"more"];
[items addObject:[item copy]];
itemsCount = itemsCount + 1;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([currentElement isEqualToString:@"name"]) {
[currentSection appendString:string];
} else if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:@"more"]) {
[currentSummary appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.progressView.view removeFromSuperview];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[dataTable reloadData];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([sections count] == 0) {
NSString * path = @"http://www.campuscloud.co.uk/_dev/calendar.xml";
[self parseXMLFileAtURL:path];
}
}
但是当我尝试在我的表中显示它时,当我为一个日期显示两个以上的事件时,一切都会出错。我希望日期显示为节标题以及要在下面显示的事件。
欢迎任何帮助...
答案 0 :(得分:1)
嘿伙计......我不知道我是否理解了coorect,但是我们去吧..
您可以创建数组字典..
NSMutableDictionary *dates = [NSMutableDictionary alloc];
NSMutableArray *events = [NSMutableArray alloc];
在数组中,您将插入一个日期的所有事件..
[events addObject:@"event 1"];
在字典中,您将插入以日期为键
的事件数组[dates setobject:events forKey:@"01/01/2010"];
每当解析器找到一个新日期时,都应该创建一个新数组(我的意思是,值是新的..不要附加数组,清理它......但是你不需要创建一个全新的数组)
当然,您必须对解析器进行一些更改以识别日期等,但这不是什么大问题。
在你的表中,每个部分都是你字典中的一个键..所以部分的数量是字典上的项目数...而部分内的值是每个日期的数组..
我希望它有所帮助..