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.

John S.
  • 31
  • 3
  • show me the xml you are looking at and what node you want. I will see if I can help. – Louie Jul 12 '11 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. – John S. Jul 12 '11 at 22:21

1 Answers1

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";
Kal
  • 24,724
  • 7
  • 65
  • 65
  • Thank you for the helpful reply. Unfortunately, it says that inArray is undeclared. – John S. Jul 12 '11 at 21:23
  • To Kal also: Additionally, when I try to call the code from the main implementation file, it complains that theMainStack is undeclared. – John S. Jul 12 '11 at 21:30
  • I edited my code. I wasn't really shooting for working code -- just some pseudocode, but maybe the edits will help. – Kal Jul 12 '11 at 21:33
  • 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 S. Jul 12 '11 at 21:36
  • @John -- better yet .. put this method inside your SimpleParser.m and write a wrapper method in your SimpleParser.h / .m .. – Kal Jul 12 '11 at 21:40
  • Just tried that, and unfortunately I get the same error. Just to make sure I did it right: I placed the wrapper method in SimpleParser.h, and the implementation in SimpleParser.M. I am trying to call it from a viewcontroller.m file. Thanks again for helping me out with this, will have it soon hopefully. – John S. Jul 12 '11 at 21:47
  • I just tried this and it seems to work for me. Try replacing theMainStack with [self getRoot] – Kal Jul 12 '11 at 21:51
  • It now compiles, but does not return a value. Just to make sure, I attempting to check by: `NSLog(@"It is: %@",[thisParser getParsedElement:@"x-position"]);` – John S. Jul 12 '11 at 22:02
  • :-) Despite the total lack of upvotes , I'll continue to help here. I made another edit .. looks like my earlier code would have just checked the first element in the returned array. – Kal Jul 12 '11 at 22:06
  • I would upvote you if it allowed me to, as you've been very helpful. Unfortunately, even with the most recent edit, still running into the same problem. (I do believe I can at least mark your answer once it works, though) – John S. Jul 12 '11 at 22:09
  • You can try putting some NSLog() statements inside the for loop to make sure its iterating through the array elements correctly. At this point, I can't really help much more without seeing what you are seeing. – Kal Jul 12 '11 at 22:16
  • It seems like nothing in the else block is getting called at all. The first block (the if) is called once. Additional, it provides a warning about reaching the end of a non-void function. – John S. Jul 12 '11 at 22:20
  • Any idea why this might be occuring? It doesn't seem as if it's actually iterating through the array at all, but I'm having some trouble deciphering your code. Would appreciate any clarification, and thank you already for your help thus far! – John S. Jul 12 '11 at 22:43
  • It now returns 'children'. I think it's getting closer... Thanks for the continued help. (Also, I think the main function is missing a closing } ?) – John S. Jul 12 '11 at 23:42
  • Yes. You should put a return nil at the bottom of the function. – Kal Jul 13 '11 at 01:38