3

I am using the XML Parser available here: http://www.saygoodnight.com/2009/08/a-simple-quick-reusable-xml-parser-for-the-iphone/

I chose it because I simply couldn't get the regular one to work with my webpage. This one does work however, as in the console I get a result like:

Element: x-position Data: 136 Parent: somepoint

This comes from code like this in my main implementation file:

[thisParser dumpRoot];

My question is, how can I actually access the value 136 somehow? It'd be great if I could just do object.x-position or something, but I'm not sure how that'd work with this class. The full class is available for download at the aforementioned link, and does seem to be a good alternative.

I would really appreciate any help, thank you.

2
  • show me the xml you are looking at and what node you want. I will see if I can help. Commented Jul 12, 2011 at 21:35
  • @Louie The URL is just a standard XML page, containing various properties that I would like to actually retrieve the values of one-by-one, not just parse the file and dump all the results out to the console. Commented Jul 12, 2011 at 22:21

1 Answer 1

1

The premise of the parser seems very straightforward. When it is done parsing, it will provide you with an NSArray of NSDictionaries.

You just have to write another method that makes use of it ..

Something like --

-(NSString*) getElement:(NSString*)element fromArray:(NSArray *)array
{
   for ( int i = 0 ; i < [array count] ; i++ ) 
   {
       NSDictionary* thisDict = [array objectAtIndex:i];
       if ( [element isEqualToString:[thisDict objectForKey:@"element"]] )
               return [thisDict objectForKey:@"data"];
       else
       {
            NSString *ret =  [self getElement:element fromArray:[thisDict objectForKey:@"children"]];
            if ( ret == nil )
                continue;
            else 
                return ret;
   }
}
return nil;
}

Put a wrapper method in your SimpleParser.h

-(NSString*)getParsedElement:(NSString *)elementName;

Implement in your .m like so--

-(NSString *)getParsedElement:(NSString *)elementName 
{
    return [self getElement:elementName fromArray:theMainStack];
}

Then you can call it like so --

[thisParser getParsedElement:@"x-position";
Sign up to request clarification or add additional context in comments.

15 Comments

Thank you for the helpful reply. Unfortunately, it says that inArray is undeclared.
To Kal also: Additionally, when I try to call the code from the main implementation file, it complains that theMainStack is undeclared.
I edited my code. I wasn't really shooting for working code -- just some pseudocode, but maybe the edits will help.
I see; thanks for fixing it up. One more problem now though, it says: "error: accessing unknown 'theMainStack' getter method" Would I want to go ahead and do the @property and @synthesize bit for that variable or something? Thanks again.
@John -- better yet .. put this method inside your SimpleParser.m and write a wrapper method in your SimpleParser.h / .m ..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.