CXMLDocument – 问题?

我正在开发epub阅读器程序。

要获取epub文件的内容,我想解析container.xml。

这里是。

      

我想获取元素,所以我使用了以下代码。

 CXMLDocument* manifestFile = [[[CXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:manifestFilePath] options:0 error:nil] autorelease]; NSArray* nodes = [manifestFile nodesForXPath:@"//rootfiles" error:nil]; NSLog(@"Nodes count is %d\n", [nodes count]); 

我得到以下结果。

 Nodes count is 0 

为什么我得到这个结果?

请帮帮我。

谢谢。

问题是您的XML具有命名空间。 您需要显式定义在使用XPath查询时要使用的命名空间。

 NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"urn:oasis:names:tc:opendocument:xmlns:container", @"oasis", nil]; NSArray* nodes = [manifestFile nodesForXPath:@"//oasis:rootfiles" namespaceMappings:dict error:nil]; NSLog(@"Nodes count is %d\n", [nodes count]); // --> Nodes count is 1