0

I am getting some XML back form a webservice:

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
  <Table>
    <CITY>Jupiter</CITY>
    <STATE>FL</STATE>
    <ZIP>33477</ZIP>
    <AREA_CODE>561</AREA_CODE>
    <TIME_ZONE>E</TIME_ZONE>
  </Table>
</NewDataSet>

I need a simple clean way to get the CITY and STATE values form this XML. Is there a nice and simple way to do this in iOS?

Slee
  • 27,498
  • 52
  • 145
  • 243
  • There are open source frameworks around for parsing XML files. However, simple structures like this one may be read using NSDictionary dictionaryWithContentOfFile. In this case you would receive a dictionary. It has one object. The object is a dictionary stored unter the key of "NewDataSet". That contains a dictionay with the key of "Table". That one is a dictionary too storing your values with the keys CITY, STATE, ZIP, AREA_CODE and TIME_ZONE. – Hermann Klecker Jul 25 '12 at 11:11
  • Strictly speaking the file read must be a property list. Property lists happen to be XML too. For more information about it refer to https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html . It should suit your case and is a pretty simple implementation. For more complex XML this may not work. It may not work for very large XML files too because the content of the file is completely stored in one Dictionary object. – Hermann Klecker Jul 25 '12 at 11:14

5 Answers5

2

Theres a neat NSXMLParser Wrapper which converts the XML file to an NSDictionary for you.

It's simple and clean!

http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/

Then from there, you could use:

NSDictionary *dict = [self convertXML:xmlContents];
NSArray *tables = [dict objectForKey:@"NewDataSet"];

for (NSDictionary *table in tables) {

   NSLog(@"City = %@", [table objectForKey:@"city"]);

}
max_
  • 24,076
  • 39
  • 122
  • 211
  • I looked at this, my project is using ARC – Slee Jul 25 '12 at 11:15
  • Disable arc for those project files then.http://stackoverflow.com/questions/6646052/how-can-i-disable-arc-for-a-single-file-in-a-project – max_ Jul 25 '12 at 11:27
1

Use xmldocument parser written using NSXMLParser but with much easier functions that the developer can use.

  1. Add SMXMLDocument.h and .m files to your project
  2. Add #import in your class implementation file (.m file)

    // create a new SMXMLDocument with the contents the xml file
    // data is the NSData representation of your XML
    SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];
    
    // Pull out the <NewDataSet> node
    SMXMLElement *dataset = [document.root childNamed:@"NewDataSet"];
    
    // Look through <Table> children
    for (SMXMLElement *table in [dataset childrenNamed:@"Table"]) {
        // demonstrate common cases of extracting XML data
        NSString *city = [table valueWithPath:@"CITY"]; // child node value
        NSString *state = [table valueWithPath:@"STATE"]; // child node value
    }
    

P.S. I have not run this code but modified to match your case based on a similar usage.

ilight
  • 1,622
  • 2
  • 22
  • 44
0

There are lost of tools to get this done (link). I would recommend the SAX method, since you only have to parse this XML data (for example NSXMLParser).

overbet13
  • 1,654
  • 1
  • 20
  • 36
0

please look at:NSXMLParser

and method: (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict

it's very simple to use. `

Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79
0

You can use NSXMLParser, I am not at my mac right now so cannon double check but basically you set up an NSXMLParser with the data that comes back. You then set up a class as a delegate to the parser, when the parser hits an element it will tell you what element it has hit and it's attributes.

If you have any control over the web service I would seriously consider using JSON data rather than XML. ObjC comes with a really good JSON parser.

Sheik Yerbouti
  • 1,054
  • 1
  • 8
  • 14