iOS开发:用TFHpple解析xml数据

对于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标签也是如此。

 

 

 

 

 

 

转载于:https://www.cnblogs.com/xszhao/p/3640895.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值