abc123...">

Objective-C XML Navigation

I get this XML response from TwitPic:

<?xml version="1.0" encoding="UTF-8"?> <rsp stat="ok"> <mediaid>abc123</mediaid> <mediaurl>http://twitpic.com/abc123</mediaurl> </rsp> 

I want to access the value in the 'mediaurl' tags.

So I tried:

 NSString *twitpicURL = [[request responseHeaders] objectForKey:@"mediaurl"]; 

But that does not work.

Can someone point me in the right direction, please, please?

Thanks,

Jamie

+4
source share
3 answers

Apple has NSXMLDocument (available only for Mac OS X) and NSXMLParser (available on Mac OS X and iPhone) are both based on the open source C library Libxml2 (available on Mac OS X and iPhone).

If you decide to use NSXMLDocument, I would suggest using XPath as a quick / easy way to get data.

+3
source

CoreFoundation has a library for parsing xml.

http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFXML/CFXML.html

If you are not developing the apple platform, expat is a well-known C XML parser :)

http://expat.sourceforge.net/

0
source

I understand what you asked a couple of days ago, but I thought I would give you an example of using NSXMLParser. I don’t remember how I found out, but this is a technique that I used in my projects.

You can create your own class like this and create it after the network call is completed and call the parseData function: or just insert all this mumbo jumbo into a class that processes network code and calls the function from is.

 #import "ThaweExampleParser.h" @interface ThaweExampleParser() <NSXMLParserDelegate> @property (nonatomic) BOOL accumulatingParsedCharacterData; @property (nonatomic, strong) NSMutableString *currentParsedCharacterData; @property (nonatomic, strong) NSMutableDictionary *parsedContents; @end @implementation ThaweExampleParser @synthesize accumulatingParsedCharacterData; @synthesize currentParsedCharacterData; @synthesize parsedContents; /* * This function you would call when your network connection finishes. * You pass in the NSMutableData you collected during the request. */ - (void)parseData:(NSData *)webData { self.currentParsedCharacterData = [NSMutableString string]; self.parsedContents = [NSMutableDictionary dictionary]; NSXMLParser *parser = [[NSXMLParser alloc] initWithData:webData]; parser.delegate = self; [parser parse]; } /* * The parsed calls this function everytime it finds a new tag. * A tag looks like "<mediaurl>" * So in this case we want to turn or BOOL to YES so that the last function collects only the characters we care about. */ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:@"mediaurl"]) { self.accumulatingParsedCharacterData = YES; [self.currentParsedCharacterData setString:@""]; } } /* * Much the previous function except the tag looks like "</mediaurl>" * Turn our BOOL to NO because we are done collecting characters for the moment. * We also are saving the important data to a dictionary so that you can use it later. */ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"mediaurl"]) { self.accumulatingParsedCharacterData = NO; [self.parsedContents setObject:[self.currentParsedCharacterData copy] forKey:elementName]; } } /* * The parser calls this function everytime it finds characters. * In your case it would call it maybe 5 or 6 times with data that you dont want to collect. * So we use a BOOL to control what we collect; */ - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if (self.accumulatingParsedCharacterData) [self.currentParsedCharacterData appendString:string]; } @end 
0
source

All Articles