0

I am working on a method to communicate between my PHP api and a iOS application. This is the reason why i wrote a function wich will get an external XML feed(of my api) and parse it.

But in the process to do that, i found a problem. The next code i wrote won't work:

-(void)getXML:(NSURL *)url {
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];
    NSURLResponse *resp = nil;
    NSError *err = nil;
    NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
    xmlDocument = [[GDataXMLDocument alloc]initWithData:response options:0 error:&error];
    NSArray *data = [[xmlDocument rootElement]elementsForName:@"api"];
    data_from_xml = [[NSMutableArray alloc]init];
    for(GDataXMLElement *e in data)
    {
        [data_from_xml addObject:e];
    }

    NSLog(@"xmlDocument:%@]\n\nData:%@\n\nData_from_xml:%@\n\nURL:%@", xmlDocument,data,data_from_xml, url);


}

The log returns:

xmlDocument:GDataXMLDocument 0x5a1afb0

Data:(null)

Data_from_xml:(
)

URL:http://sr.site-project.nl/api/?t=store.search.keyword

So, it seems that GDataXMLDocument has the XML. But i can't load it with the elementsForName argument?.

Does someone see what the problem is?

The XML:

<?xml version="1.0"?> 
<api><type>core.error</type><errorMessage>API store.search.keyword doesn't exists</errorMessage></api> 
Timo
  • 7,195
  • 7
  • 24
  • 25

1 Answers1

3

The api node is your root element:

NSArray *data = [[xmlDocument rootElement]elementsForName:@"api"];

Try:

NSError *error = nil;
NSArray *data = [xmlDocument nodesForXPath:@"//api" error:&error];

I personally prefer the nodesForXPath method for retrieving elements.

InsertWittyName
  • 3,930
  • 1
  • 22
  • 27
  • Thanx, it works(the nodesForXPath). I have some other problems with comparing a string with a string from the xml feed, but I think i can solve that by myself;) – Timo Jul 01 '11 at 18:38
  • 1
    Thanks for the accept. To get the string from an element simply use the stringValue method on the element. To compare strings use isEqualToString. Hope this helps! – InsertWittyName Jul 01 '11 at 18:42