Parse XML Files in Style
Posted by: jamesj3k In: Development
One would think that parsing and generating xml files using the iPhone SDK would be a simple task. However, Apple decided to leave out the NSXML and related classes from the SDK. So- out of the box, that leaves us with NSXMLParser. No thanks.
My first question was: How can I deal with xml the way I'm used to- the way C#, ActionScript, and to a lesser extent JavaScript handle it. The answer is the wonderful KissXML package.
Now we can do things like this:
NSString * fileContents = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error:nil]; DDXMLDocument * doc = [[DDXMLDocument alloc] initWithXMLString: fileContents options:0 error:nil]; DDXMLElement * root = [doc rootElement]; NSDictionary * mapAttribs = [root attributesAsDictionary]; mapSize.width = [[mapAttribs valueForKey:@"width"] intValue]; mapSize.height = [[mapAttribs valueForKey:@"height"] intValue]; DDXMLElement * tilemap = [[root elementsForName:@"tilemap"] objectAtIndex:0]; NSString * source = [[tilemap attributeForName:@"source"] stringValue];
Not as good as C#, but much better than NSXMLParser. Happy XMLing.
No comments:
Post a Comment