Hi there!
I'm new to coding with Obj C and on the iPhone SDK. I'm having a little trouble understanding how NSXMLParser works. I used this
NSXMLParser Simple Problem - Mac Forums as an example.
Basically I have an xml file that looks like this:
Code:
<?xml version="1.0" encoding="windows-1250"?>
<RootElement>
<State>
<Name>Arizona</Name>
<Time>6 a.m.ñ2 a.m.</Time>
<Time>6 a.m.</Time>
<Notes>This is a note for Ar</Notes>
</State>
<State>
<Name>California</Name>
<Time>CA Time.</Time>
<Time>CA Time2.</Time>
<Notes>This is a note for California</Notes>
</State>
</RootElement>
I'm trying to load this xml file from awakeAtNib, and load an NSMutableDictionary with key as name of each state, and value as an array containing all the values for time, time and notes.
When I actually do [xmlParser parse], nothing happens! Am I supposed to override the "parse" method somewhere?
Since it did not do anything, I explicitly called the two parser methods.
My delegate code looks like this:
Code:
// XMLParserAppDelegate.m
// XMLParser
//
// Created by Mansi Gandhi on 2/16/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "XMLParserAppDelegate.h"
@implementation XMLParserAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (void)awakeFromNib
{
NSLog(@"Awake from Nib");
NSString *filePath = [[NSString stringWithString:@"~/Desktop/Sample.xml"]stringByStandardizingPath];
[self parseXMLFile:filePath];
}
- (void)dealloc {
[window release];
[super dealloc];
}
- (void)parseXMLFile:(NSString *)pathToFile {
BOOL success;
NSURL *xmlURL = [NSURL fileURLWithPath:pathToFile];
if (xmlParser)
[xmlParser release];
xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities:YES];
success = [xmlParser parse];
[self parser:xmlParser didStartElement:@"RootElement" attributes:dictionary];
[self parser:xmlParser didEndElement:@"RootElement"];
NSLog(@"End parsing!");
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName attributes:(NSDictionary *)attributeDict
{
if ( [elementName isEqualToString:@"RootElement"]) {
NSLog(@"found rootelement");
[self parser:xmlParser didStartElement:@"State" attributes:dictionary];
return;
}
if ( [elementName isEqualToString:@"State"] ) {
NSLog(@"found state");
return;
}
if ( [elementName isEqualToString:@"Name"] ) {
NSLog(@"found name");
return;
}
if ( [elementName isEqualToString:@"Time"] ) {
NSLog(@"found time");
return;
}
if ( [elementName isEqualToString:@"Notes"] ) {
NSLog(@"found notes");
return;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"found characters:%@",string);
//Dont know what to do here!
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
{
if ([elementName isEqualToString:@"RootElement"]) {
NSLog(@"rootelement entering...");
}
if ([elementName isEqualToString:@"State"]) {
NSLog(@"state entering...");
}
if ([elementName isEqualToString:@"Name"]) {
NSLog(@"name entering...");
}
if ([elementName isEqualToString:@"Time"]) {
NSLog(@"time entering...");
}
if ([elementName isEqualToString:@"Notes"]) {
NSLog(@"notes entering...");
}
}
@end
I don't know where I can get the value for that element. Like how do I get "CA Time." for <Time> under California, and then put it into a NSMutable Array.
Also, the way I call it obviously doesn't recurse for each state.
Any help is appreciated!
Thanks
Mansi