代码改变世界

Libxml2中使用xpath解析xml问题

2011-04-06 14:51  cnb_mtime  阅读(684)  评论(0编辑  收藏  举报
//hack to remove xmlns => avoid xpath search not works  
//如果节点xml节点含有xmlns,这个时候用xpath是匹配不出节点内容的,只好删掉了,找这个原因不容易啊 - -|||
xmlStr = [xmlStr stringByReplacingOccurrencesOfString:@"xmlns" withString:@"noNSxml"];
NSMutableArray
* contents = [NSMutableArray array];

NSError
* error = nil;
DDXMLDocument
* xmlDoc = [[DDXMLDocument alloc] initWithXMLString:xmlStr options:0 error:&error];

if (error) {
NSLog(
@"%@",[error localizedDescription]);
return nil;
}

NSArray
* resultNodes = nil;
resultNodes
= [xmlDoc nodesForXPath:@"//audio | //text | //image | //img" error:&error];

if (error) {
NSLog(
@"%@",[error localizedDescription]);
return nil;
}

for(DDXMLElement* resultElement in resultNodes)
{
NSString
* name = [resultElement name];
//audio , text or other media type
NSString* fileName = [[resultElement attributeForName:@"src"] stringValue];
// 0.txt
}