what type of XML file is this?

175 Views Asked by At

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"/>
2

There are 2 best solutions below

4
On BEST ANSWER

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. :-)

0
On

That looks like a home-grown flavor of XML. It’s not uncommon to generate data as XML that isn’t actually a standards-based grammar.

The reason for this is because XML is easy to parse—both because of the syntactical rules, and because the XML ecosystem is mature and has lots of tools for parsing, among other things.

Just because it’s not part of an official grammar, your data isn’t useless! You could use XSLT to transform it to another XML grammar that is official. For starters, I think SVG is your best bet—you’Ve got lots of very specific point data that could be useful in an SVG path.