Eugene Trapeznikov
Eugene Trapeznikov

Reputation: 3240

Can't parse XML with NSXMLParser

I got an iOS app. All data stores in xml files.

<?xml version="1.0" encoding="utf-8"?>
<objects>
    <object 
        name="Скульптура Р. Исмагилова «Пермяк – соленые уши»" 
        xcoord="58.00998487478568" 
        ycoord="56.239829063415527"
        type="" 
        description="" 
        external_name="gl1" />
</objects>

So, i want to parse this XML file to NSMutableArray

I do it with NSXMLParser deleagate class. This is class's code:

@implementation XMLParser

@synthesize aPLace;

- (XMLParser *) initXMLParser {

    [super init];

    appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];

    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)attributeDict {

    NSLog(@"parsing");

    if([elementName isEqualToString:@"objects"]) {
        //Initialize the array.
        appDelegate.books = [[NSMutableArray alloc] init];
    }
    else if([elementName isEqualToString:@"object"]) {

        //Initialize the book.
        aPLace = [[Place alloc] init];

        //Extract the attribute here.
        aPLace.name = [[attributeDict objectForKey:@"name"] string];

        NSLog(@"Reading name :%@", aPLace.name);
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if([elementName isEqualToString:@"objects"])
        return;

    if([elementName isEqualToString:@"object"]) {
        [appDelegate.greenLine addObject:aPLace];
    }

}

@end

You can see that there is NSLog(@"parsing"); in didStartElement function. But output is empty.

So, i implement parsing in AppDelegateClass's function applicationDidFinishLaunching

- (void)applicationDidFinishLaunching:(UIApplication *)application {


    NSURL *url = [[NSURL alloc] initWithString:@"greenline_ru.xml"];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

    //Initialize the delegate.
    XMLParser *parser = [[XMLParser alloc] initXMLParser];

    //Set delegate
    [xmlParser setDelegate:parser];

    //Start parsing the XML file.
    BOOL success = [xmlParser parse];

    if(success)
        NSLog(@"No Errors");
    else
        NSLog(@"Error Error Error!!!");

    // Configure and show the window
    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
}

and every time output is Error Error Error!!! why? What's the problem?

Upvotes: 0

Views: 1159

Answers (2)

Ganzolo
Ganzolo

Reputation: 1394

NSURL *url = [[NSURL alloc] initWithString:@"greenline_ru.xml"];

I think this is your problem. If you have file you should use something like

NSString *path = [[NSBundle mainBundle] pathForResource:@"greenline_ru"
                                                 ofType:@"xml"];
[[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:path]]

Upvotes: 2

Naina Soni
Naina Soni

Reputation: 720

Use TBXML, this is quite easy to use... Study this- http://www.tbxml.co.uk/TBXML/API.html

Upvotes: 0

Related Questions