我正在尝试使用RaptureXML解析ePub文档中的文件。我可以从子节点遍历XML文档,但是当我尝试通过XPath获取元素时,它不会被返回。
这是XML(container.xml):
<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
这是我的解析代码:
NSString *containerPath = [path stringByAppendingPathComponent:@"META-INF/container.xml"];
NSData *containerData = [NSData dataWithContentsOfFile:containerPath];
RXMLElement *containerRoot = [RXMLElement elementFromXMLData:containerData];
NSLog(@"root %@ ", [containerRoot tag]);
RXMLElement *rootfiles = [containerRoot child:@"rootfiles"];
NSLog(@"root %@ ", [rootfiles tag]);
RXMLElement *rootfile = [rootfiles child:@"rootfile"];
NSLog(@"root %@ ", [rootfile tag]);
NSLog(@"attr %@ ", [rootfile attribute:@"full-path"]);
[containerRoot
iterateWithRootXPath:@"//rootfile"
usingBlock:^(RXMLElement *elem) {
NSLog(@"path=%@", [elem attribute:@"full-path"]);
}];
/* I've also tried this.... */
[rootfiles
iterateWithRootXPath:@"//rootfile"
usingBlock:^(RXMLElement *elem) {
NSLog(@"path=%@", [elem attribute:@"full-path"]);
}];
这个输出是:
2013-01-14 22:03:43.299 MLReader[9027:c07] root container
2013-01-14 22:03:43.299 MLReader[9027:c07] root rootfiles
2013-01-14 22:03:43.300 MLReader[9027:c07] root rootfile
2013-01-14 22:03:43.300 MLReader[9027:c07] attr content.opf
正如您所看到的,当我从一个孩子搬到另一个孩子时,我可以找到我正在寻找的rootfile元素。但是,永远不会输入iterateWithRootXPath:usingBlock:中的块。
我已经在Java中完成了大量的XML / XPath,但我是Objective-C / iOS的新手。