解析youtube api feed v2

时间:2012-07-17 02:19:44

标签: objective-c ios xml xpath touchxml

我正在尝试解析YouTube数据API提供的YouTube Feed。此Feed会列出所有视频uploaded by a specific user,如下所示:https://gdata.youtube.com/feeds/api/users/google/uploads?v=2

我在iOS应用中使用touchXML。我尝试使用CXMLDocument中的nodesForXPath函数收集所有<entry>个节点。

NSURL *url = [NSURL URLWithString: newsFeedUrl];
CXMLDocument *feedParser = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil];
NSArray *resultNodes = [feedParser nodesForXPath:@"//entry" error:nil];
NSLog(@"%@",resultNodes.count); // >>> return (null)

但是这个查询不会返回任何内容。我检查了xpath语法;我认为这不对。

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。它是关于Xpath和命名空间的。为了使用xpath解析XML文档,我必须为我想要支持的每个命名空间创建一个默认映射。

NSDictionary * maps = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"http://www.w3.org/2005/Atom",@"http://search.yahoo.com/mrss/",@"http://a9.com/-/spec/opensearch/1.1/",@"http://schemas.google.com/g/2005",@"http://gdata.youtube.com/schemas/2007",@"W/\"C08MQXg-cSp7I2A9WhJRFEs.\"",nil] forKeys:[NSArray arrayWithObjects: @"ns",@"media",@"openSearch",@"gd",@"yt",@"etag",nil]];
NSArray *resultNodes = [feedParser nodesForXPath:@"//ns:entry" namespaceMappings:maps error:nil];

TouchXML的更多解释。 我找到了一篇关于GDataXML的帖子,可能会有用。