ios中xml和html解析(封装)

下载地址  http://pan.baidu.com/share/link?shareid=2902188921&uk=923776187

GDataXML和TFHpple配置是一样的(配置方式参考 http://blog.csdn.net/ryantang03/article/details/7868246)

主要封装代码

html封装

ios中xml和html解析(封装)

#import <Foundation/Foundation.h>
#import "TFHpple.h" @interface JSHpple : NSObject
+(id)ShareHpple;
-(NSArray *)HtmlWithData:(NSData *)data XPath:(NSString *)path;
@end #import "JSHpple.h" @implementation JSHpple +(id)ShareHpple{
static dispatch_once_t onceToken;
static JSHpple *hpple=nil;
dispatch_once(&onceToken, ^{
hpple=[[JSHpple alloc] init];
});
return hpple;
} -(NSArray *)HtmlWithData:(NSData *)data XPath:(NSString *)path{
TFHpple *happen=[[TFHpple alloc] initWithHTMLData:data];
NSArray *htmlContent=[happen searchWithXPathQuery:path];
NSMutableArray *result=[NSMutableArray arrayWithCapacity:];
if (htmlContent.count<=) {
NSLog(@"没有解析到对应的数据");
return nil;
}
for (TFHppleElement *ele in htmlContent) {
NSString *content=[self SearchElement:ele];
if (!content) {
content=@"Null";
}
[result addObject:content];
} return result; } -(NSString *)SearchElement:(TFHppleElement *)element{
NSArray *childs=[element children];
if (childs.count==) {
if ([[childs lastObject] isTextNode]) {//没有子节点
return [element text];
}
else{//有子节点
TFHppleElement *childElement=[childs lastObject];
NSString *content=[self SearchElement:childElement];
if (content) {
return content;
}
}
}
return nil;
} @end

xml  -->GDataxml的用法 (通用)

ios中xml和html解析(封装)

#import <Foundation/Foundation.h>
#import "GDataXMLNode.h" @interface JSDataXml : NSObject +(NSArray *)ParseXmlWithPath:(NSData *)data XPath:(NSString *)path attributes:(NSArray *)attributeArrarys;
@end #import "JSDataXml.h" @implementation JSDataXml +(NSArray *)ParseXmlWithPath:(NSData *)data XPath:(NSString *)path attributes:(NSArray *)attributeArrarys{ NSError *error=nil;
//开始解析
GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithData:data options: error:&error];
if (error) {
NSLog(@"xml解析失败-->%@",[error localizedDescription]);
return nil;
}
//得到根元素
GDataXMLElement *rootElement=[doc rootElement];
//利用xpath找元素 NSArray *Elements=[rootElement nodesForXPath:path error:&error];
if (error) {
NSLog(@"xpath有误-->%@",[error localizedDescription]);
return nil;
}
if (Elements.count<=) {
NSLog(@"没有匹配到数据");
return Elements;
} NSMutableArray *result=[NSMutableArray array];
//遍历匹配到的每个元素
for (int i=; i<Elements.count; i++) {
GDataXMLElement *element=Elements[i];
if (attributeArrarys.count>) {//如果节点有元素
NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:];
for (int i=; i<attributeArrarys.count; i++) {
NSString *key=attributeArrarys[i];
NSString *value=[[element attributeForName:key] stringValue];
if (!value) {
value=@"NULL";
}
[dic setObject:value forKey:key]; }
[result addObject:dic];
}
else{//如果节点没有元素,默认就返回属性值
NSString *str= [element stringValue];
[result addObject:str];
} } return result;
} @end

xml 第二种封装(一个子节点没有属性)推荐使用

ios中xml和html解析(封装)

只适合这种xml数据格式

<?xml version="1.0" encoding="utf-8"?>
<Users>
<User id="">
<name>Ryan</name>
<age></age>
</User>
<User id="">
<name>Tang</name>
<age></age>
</User>
</Users>
@protocol UseXmlParser <NSObject>

@optional
- (void)NetWorkBackConnectID:(int)connectID BackDic:(NSMutableDictionary*)aBackDic WithNetState:(int)netState; @end @interface UseXmlParser : NSObject<NSXMLParserDelegate>
{
id aTarget;
int aConnectId; NSMutableData *parData; NSXMLParser *xmlParser; NSMutableArray *dicNameArray;
BOOL endStringElement;
NSMutableString *keyString;
NSMutableDictionary *xmlDataSource;
}
-(id)initWithParserData:(NSData*)pData target:(id)toTarget connectId:(int)bConnectId; -(void)parser; @end #import "UseXmlParser.h" @implementation UseXmlParser -(id)initWithParserData:(NSData*)pData target:(id)toTarget connectId:(int)bConnectId{
if (self = [super init]) { aTarget = toTarget;
aConnectId = bConnectId; dicNameArray = [[NSMutableArray alloc] initWithCapacity:];
keyString = [[NSMutableString alloc] initWithCapacity:];
xmlDataSource=[[NSMutableDictionary alloc] initWithCapacity:];
endStringElement = FALSE; parData = [[NSMutableData alloc] initWithData:pData]; }
return self;
}
-(void)parser{
xmlParser = [[NSXMLParser alloc] initWithData:parData];
[xmlParser setDelegate:self];
[xmlParser parse];//开始解析
[xmlParser release];
} -(NSMutableDictionary*) getDic {
NSMutableDictionary *dic=xmlDataSource;
for (int i=; i<[dicNameArray count]; i++) {
dic=[dic objectForKey:(NSString*)[dicNameArray objectAtIndex:i]];
}
return dic;
}
- (NSString*)getListElement:(NSString*)elementName From:(NSArray*)array {
NSString *temp = elementName;
for (int i=; i<; i++) {
if ([array containsObject:elementName]) {
elementName = [NSString stringWithFormat:@"%@%d", temp, i+];
}
else {
return elementName;
}
}
return nil;
}
- (id)getStringByKey:(NSString*)key {
NSArray *array = [key componentsSeparatedByString:@"/"];
NSDictionary *dic = [NSDictionary dictionary];
for (int i=; i<[array count]-; i++) {
dic = [dic objectForKey:[array objectAtIndex:i]];
}
NSString *temp = [dic objectForKey:[array objectAtIndex:[array count]-]];
if (!temp) {
temp = @"";
}
return temp;
}
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([dicNameArray count]>) {
NSMutableDictionary *dic = [self getDic];
elementName = [self getListElement:elementName From:[dic allKeys]]; [dic setObject:[[NSMutableDictionary alloc] init] forKey:elementName];
[dicNameArray addObject:elementName];
}
else {
[xmlDataSource setObject:[[NSMutableDictionary alloc] init] forKey:elementName];
[dicNameArray addObject:elementName];
} endStringElement = TRUE;
[keyString setString:@""];
} -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
[keyString appendString:string];
}
- (NSMutableDictionary*)getLastDic {
NSMutableDictionary *dic=xmlDataSource;
for (int i=; i<[dicNameArray count]-; i++) {
dic=[dic objectForKey:(NSString*)[dicNameArray objectAtIndex:i]];
}
return dic;
}
-(void) removeLastDic {
if ([dicNameArray count]>) {
[dicNameArray removeLastObject];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if (endStringElement) {
if (![keyString isEqualToString:@"\n"]) {
if ([keyString hasPrefix:@"\n"]) {
[keyString setString:[keyString substringFromIndex:]];
}
NSMutableDictionary *dic = [self getLastDic];
[dic setObject:[NSString stringWithString:keyString] forKey:[dicNameArray objectAtIndex:[dicNameArray count]-]];
}
} [keyString setString:@""];
endStringElement = FALSE;
[self removeLastDic]; }
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@" \nxml解析返回-->> %@",xmlDataSource);
if (aTarget && [aTarget respondsToSelector:@selector(NetWorkBackConnectID:BackDic:WithNetState:)]) {
[aTarget NetWorkBackConnectID:aConnectId BackDic:xmlDataSource WithNetState:];
}
} -(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
NSLog(@"--error-->%@",parseError.localizedDescription);
}
-(void)dealloc{ [dicNameArray release];
[keyString release];
[xmlDataSource release];
[super dealloc];
}
@end

三种使用方式

#import "ViewController.h"
#import "JSDataXml.h"
#import "JSHpple.h"
@interface ViewController (){
//配置GDDataXML和TFHpple是相同的。
//配置信息 http://blog.csdn.net/ryantang03/article/details/7868246
} @end @implementation ViewController #pragma mark -解析html - (IBAction)Htmlclick:(id)sender {
NSString *path=[[NSBundle mainBundle] pathForResource:@"" ofType:@"html"];
NSData *data=[[NSData alloc] initWithContentsOfFile:path];
NSArray *result= [[JSHpple ShareHpple] HtmlWithData:data XPath:@"//td"];
NSLog(@"--%@",result); } #pragma mark -解析一个节点一个属性格式的xml
- (IBAction)ParseXmlOneclick:(id)sender {
NSString *path=[[NSBundle mainBundle] pathForResource:@"" ofType:@"xml"];
NSData *data=[[[NSData alloc] initWithContentsOfFile:path] autorelease];
UseXmlParser *xmlParse=[[UseXmlParser alloc] initWithParserData:data target:self connectId:];
[xmlParse parser];
} - (void)NetWorkBackConnectID:(int)connectID BackDic:(NSMutableDictionary*)aBackDic WithNetState:(int)netState{
NSLog(@"-connectId-->%zi-->%@",connectID, aBackDic);
} #pragma mark -通用方式
- (IBAction)ParseXmlMangeclick:(id)sender {
// #pragma mark -解析xml第一种格式
[self ParseXmlMethod1];
// #pragma mark -解析xml第二种格式
[self ParseXmlMethod2];
} #pragma mark -解析xml第一种格式
-(void)ParseXmlMethod1{
NSString *path=[[NSBundle mainBundle] pathForResource:@"" ofType:@"xml"];
NSData *data=[[NSData alloc] initWithContentsOfFile:path];
NSArray *attribute=@[@"wait_count",@"max_wait_time",@"branch_name",@"branch_id"];
NSArray *result= [JSDataXml ParseXmlWithPath:data XPath:@"//item" attributes:attribute];
NSLog(@"__%@-->%zi",result,result.count);
} #pragma mark -解析xml第二种格式
-(void)ParseXmlMethod2{
NSString *path=[[NSBundle mainBundle] pathForResource:@"" ofType:@"xml"];
NSData *data=[[NSData alloc] initWithContentsOfFile:path]; NSArray *result= [JSDataXml ParseXmlWithPath:data XPath:@"//name" attributes:nil];
NSLog(@"__%@-->%zi",result,result.count);
}
@end
上一篇:【IOS】 XML解析和xml转plist文件(GDataXML)


下一篇:解决CentOS虚拟机克隆后无法上网(网卡信息不一致)的问题