开发者

Getting external XML with GDataXMLNode for Objective-C?

开发者 https://www.devze.com 2023-03-16 15:20 出处:网络
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.

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 n开发者_如何学Pythonext 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> 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消