对于xml的解析有多种方法,其中之一就是使用TFHpple。下面就自己的项目解析谈谈如何使用这个类。首先下载下面的代码:
http://code4app.com/ios/解析HTML/5167ca396803faf447000002
加入三个类TFHpple.h,TFHpple.m,TFHppleElement.h,TFHppleElement.m,XPathQuery.h,XPathQuery.m加到自己的项目中,在Frameworks中导入libxml2.x
然后需要修改Header Search Paths
最后,看看自己的代码:
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData]; NSArray *elements = [xpathParser searchWithXPathQuery:@"//a"]; for (TFHppleElement *element in elements) { if ([element attributes]) { newsDetailsModel.content =[[element attributes]objectForKey:@"href"]; } }
这个elements是a标签的内容,你可以打印一下看看,包含啥内容,我是取href,连接 内容
其实,element也有好多东西,
@interface TFHppleElement : NSObject { @private NSDictionary * node; } - (id) initWithNode:(NSDictionary *) theNode; + (TFHppleElement *) hppleElementWithNode:(NSDictionary *) theNode; // Returns this tag's innerHTML content. @property (nonatomic, readonly) NSString *content; // Returns the name of the current tag, such as "h3". @property (nonatomic, readonly) NSString *tagName; // Returns tag attributes with name as key and content as value. // href = 'http://peepcode.com' // class = 'highlight' @property (nonatomic, readonly) NSDictionary *attributes; // Returns the children of a given node @property (nonatomic, readonly) NSArray *children; // Returns the first child of a given node @property (nonatomic, readonly) TFHppleElement *firstChild; // the parent of a node @property (nonatomic, retain, readonly) TFHppleElement *parent; // Provides easy access to the content of a specific attribute, // such as 'href' or 'class'. - (NSString *) objectForKey:(NSString *) theKey; @end
有属性,子标签,父标签,第一个标签,节点等等,相信看到这个内容后,加上自己的打印数据,应该可以获取到你想要的数据了。
如果你获取的是p标签,那么把a改成p就可以了,img标签也是如此。