Две небольшие вещи для рассмотрения:
Вы обязательно должны прочитать документацию Objective-C / Cocoa.
В противном случае вы никогда не сможете что-то программировать самостоятельно.
В конце концов, это будетсэкономить вам массу времени на борьбе с подобными проблемами.
То, что вы спрашиваете, - это просто основы программирования.
(Помимо части XML)
Вы никогда не должныПопросите людей написать полные приложения.
Люди из Переполнения стека более чем готовы помочь с конкретными проблемами,
, но они не работают для вас.
Как говорится, здесь мойфактический ответ:
В предоставленном вами XML-коде содержится синтаксическая ошибка:
The second <Description> should be </Description>
В качестве примера кода ниже я использовал следующий XML-файл:
<?xml version="1.0" ?>
<Report>
<Date>20110311</Date>
<Title>The Title</Title>
<Description>Description sample</Description>
</Report>
Этот быстро написанный пример делает все, что вам нужно.
Создайте новое приложение с XCode и откройте файл ..... AppDelegate.m.
Просто вставьте код Objective C, упомянутый ниже, вследующая функция:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Paste Here
}
Далее нажмите «Щелкните« Создать и запустить ».
Выберите файл в NSOpenPanel и нажмите кнопку открытия».
Теперь вы должны увидеть простой диалог с результатами.
Надеюсь, это поможет вам понять, как анализировать файл XML.
Я могу представить, что 4 дня борьбы должны быть неприятными:)
Пример кода Objective C:
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
NSArray *fileTypes = [NSArray arrayWithObjects:@"xml",nil];
NSInteger result = [openPanel runModalForDirectory:NSHomeDirectory() file:nil types:fileTypes ];
if(result == NSOKButton){
NSString * input = [openPanel filename];
NSXMLDocument* doc = [[NSXMLDocument alloc] initWithContentsOfURL: [NSURL fileURLWithPath:input] options:0 error:NULL];
NSMutableArray* dates = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* titles = [[NSMutableArray alloc] initWithCapacity:10];
NSMutableArray* descriptions = [[NSMutableArray alloc] initWithCapacity:10];
NSXMLElement* root = [doc rootElement];
NSArray* dateArray = [root nodesForXPath:@"//Date" error:nil];
for(NSXMLElement* xmlElement in dateArray)
[dates addObject:[xmlElement stringValue]];
NSArray* titleArray = [root nodesForXPath:@"//Title" error:nil];
for(NSXMLElement* xmlElement in titleArray)
[titles addObject:[xmlElement stringValue]];
NSArray* descriptionArray = [root nodesForXPath:@"//Description" error:nil];
for(NSXMLElement* xmlElement in descriptionArray)
[descriptions addObject:[xmlElement stringValue]];
NSString * date = [dates objectAtIndex:0];
NSString * title = [titles objectAtIndex:0];
NSString * description = [descriptions objectAtIndex:0];
NSString *output = [NSString stringWithFormat:@"Date: %@\nTitle: %@\nDescription: %@", date, title, description];
NSRunAlertPanel( @"Result", output, @"OK", nil, nil );
[doc release];
[dates release];
[titles release];
[descriptions release];
}
Вот некоторая дополнительная информация: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/XMLParsing/Articles/UsingParser.html