使用 Objective-C 的 GDataXMLNode 获取外部 XML?
我正在研究一种在 PHP api 和 iOS 应用程序之间进行通信的方法。这就是为什么我编写了一个函数,它将获取(我的 api 的)外部 XML feed 并解析它。
但在做的过程中,我发现了一个问题。我编写的下一个代码将不起作用:
-(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);
}
日志返回:
xmlDocument:GDataXMLDocument 0x5a1afb0
Data:(null)
Data_from_xml:(
)
URL:http://sr.site-project.nl/api/?t=store.search.keyword
因此,GDataXMLDocument 似乎具有 XML。但我无法使用 elementsForName 参数加载它?
有人看到问题是什么吗?
XML:
<?xml version="1.0"?>
<api><type>core.error</type><errorMessage>API store.search.keyword doesn't exists</errorMessage></api>
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>
api
节点是您的根元素:尝试:
我个人更喜欢使用
nodesForXPath
方法来检索元素。The
api
node is your root element:Try:
I personally prefer the
nodesForXPath
method for retrieving elements.