i have a file with the points for all the states. i want to parse it so i can create an overlay for each state. i think its xml but not a traditional one with element_data format.
somebody mentioned its a plist xml.
whats the best way to parse this type of data:
<states>
<state name ="Alaska" colour="#ff0000" >
<point lat="70.0187" lng="-141.0205"/>
<point lat="70.1292" lng="-141.7291"/>
<point lat="70.4515" lng="-144.8163"/>
<point lat="70.7471" lng="-148.4583"/>
<point lat="70.7923" lng="-151.1609"/>
<point lat="71.1470" lng="-152.6221"/>
<point lat="71.1185" lng="-153.9954"/>
<point lat="71.4307" lng="-154.8853"/>
<point lat="71.5232" lng="-156.7529"/>
<point lat="71.2796" lng="-157.9449"/>
<point lat="71.2249" lng="-159.6313"/>
<point lat="70.6363" lng="-161.8671"/>
<point lat="70.0843" lng="-163.5809"/>
<point lat="69.3028" lng="-165.2399"/>
<point lat="69.1782" lng="-166.8768"/>
<point lat="68.3344" lng="-168.0414"/>
Code :
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"data"
ofType:@"xml"];
NSString* xmlStr = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:nil];
NSXMLDocument* xml = [[NSXMLDocument alloc] initWithXMLString:xmlStr
options:NSXMLDocumentTidyXML
error:nil];
NSMutableDictionary* states = [xml getChildrenInDictionary:@"states"];
The getChildrenInDictionary
method is declared in a category I've written for NSXMLDocument
.
NSXMLDocument Category :
- (NSDictionary*)getChildrenInDictionary:(NSString *)path
{
NSMutableDictionary* result = [NSMutableDictionary dictionary];
NSArray* nodes = [[[self nodesForXPath:path error:nil] objectAtIndex:0] children];
for (NSXMLElement* element in nodes)
{
[result setValue:[element stringValue]
forKey:[element name]];
}
return result;
}
Sidenote : For me, it's a perfectly valid XML file, and as such it should be processed. :-)