30 Aug, 2010
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.
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.