1

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"/>
Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
Padin215
  • 7,444
  • 13
  • 65
  • 103
  • 3
    xml is xml... you'd run this through a DOM parser like any other xml. – Marc B Apr 10 '12 at 20:46
  • Like Marc B says... it's a completely valid XML format and there's nothing really special about it. In this case, the critical data is stored in attributes instead of elements. You'd parse it out like any other XML file, reading the attributes of each of element as you do. – Matt van Andel Apr 10 '12 at 20:50

2 Answers2

2

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

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • ok, i saw another SO question that suggested a different way to handling a plist xml: http://stackoverflow.com/questions/2207404/iphone-can-we-parse-plistxml-file-using-nsxmlparser and was wondering if their suggestions would be better solutions. – Padin215 Apr 10 '12 at 21:01
  • 1
    @Log139 `Property Lists` are another thing whatsoever. Yes, they ARE xml files, too. But their format is very specific (https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/UnderstandXMLPlist/UnderstandXMLPlist.html). And your XML sample is most definitely NOT `plist`-compatible. (IF you could convert the above into a valid `plist`, then sure you could load it up with something as simple as : `NSDictionary* dic = [[NSDictionary alloc] initWithContentsOfFile:filename]`) – Dr.Kameleon Apr 10 '12 at 21:04
  • ok, great, thanks for the quick response. first time really working with an XML file so trying to figure things out. – Padin215 Apr 10 '12 at 21:06
  • @Log139 You're welcome, my friend! :-) I've had a lot of trouble working with xml files before, so feel free to ask me whatever you need. (btw, I've also written other xml functions like `getChildrenInArray` or `getChildrenInDictionary:UsingIndex:`, so if you need anything, let me know... ;-)) – Dr.Kameleon Apr 10 '12 at 21:08
1

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.

Zearin
  • 1,474
  • 2
  • 17
  • 36