user425243
user425243

Reputation:

Unable To Parse Using NSXML Parser

I have the following XML file :

<?xml version="1.0"? encoding="UTF-8"?>
<api>
        <count count="55" />
        <spa>
            <opt>aa</opt>
            <opt>bb</opt>
            <opt>cc</opt>

        </spa>
</api>

M using the following lines of code :

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"space.xml"];

NSData *data = [[NSData alloc] initWithContentsOfFile:path];

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];

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

//Set delegate
[xmlParser setDelegate:theParser];

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

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

However, m getting the output as "error error error" in gdb. I am new to Objective C and unable to get through the error. Can someone please help ?? Thanks.

Upvotes: 0

Views: 178

Answers (2)

palpoms
palpoms

Reputation: 96

Have you checked if 'path' or 'data' are nil?

Also, if 'parse' didn't succeed, you can use the method 'parserError' to get an NSError object that will hold more information about the problem. From NSXMLParser class reference:

parserError
Returns an NSError object from which you can obtain information about a parsing error.

- (NSError *)parserError

Discussion
You may invoke this method after a parsing operation abnormally terminates to determine the cause of error.

Availability
Available in iOS 2.0 and later.

After this, you should be able to call the method 'localizedDescription' on your NSError* object to get more information about the problem.

I hope this helps!

Upvotes: 1

superGokuN
superGokuN

Reputation: 1424

// Try to use NSXMLParser

NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"YourURL"]];
[parser setDelegate:self];
[parser parse];

// Below are the delegates which will get you the Data

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

  {    
            if([elementName isEqualToString:@"spa"]){
           got = YES; //got is a BOOL and here we have encountere start tag i.e <spa>
     }
  }

    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
     {
          if(got)
          {
                 NSLog(@"the Data is = %@",string);
          }
     }

    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
     {
         if([elementName isEqualToString:@"spa"])
        {
           got = NO; //Here we have encountered the end tag </spa>
        }

     }

Upvotes: 1

Related Questions