SteppingHat
SteppingHat

Reputation: 1372

Objective C get XML from URL

So after scouring the internet for multiple days and endless night grinding my forehead against a cheese-grater I come to stackoverflow with a question that may have been asked before, but I just can't seem to wrap my head around it.

Simply what I want to achieve is to get an XML file from a remote location on a web server with a URL and then turn it into an array.

So far, this is what I've gotten up to given multiple website's and documentation.

NSURL *xmlString = [NSURL URLWithString:@"http://localhost/query.xml"];

NSDictionary *dictionary=[[NSDictionary alloc] initWithContentsOfURL:xmlString];

NSLog(@"XML is %@",dictionary);

NSArray *XMLarray=[[dictionary valueForKey:@"Result"] valueForKey:@"Table"];

and the output in the console is

XML is (null)

What am I doing wrong and how do I fixey?

Thanks in advance :D

Upvotes: 1

Views: 2105

Answers (1)

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

There is a classic bit of code which does this: Simple XML to NSDictionary Converter.

There are a few implementations of this on github

It's pretty easy:

NSURL *URL = [NSURL URLWithString:@"http://localhost/query.xml"];
NSData *data = [[NSData alloc] initWithContentsOfURL:URL];
NSError *error = nil;

NSDictionary *dictionary = [XMLReader dictionaryForXMLData:data error:&error];

Hope that helps.

Upvotes: 5

Related Questions