Problem with XML Parser
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 开发者_StackOverflow中文版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.
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";
精彩评论