ios中xml和html解析(封装)
2013-08-22 18:38 甘超波 阅读(5178) 评论(0) 编辑 收藏 举报下载地址 http://pan.baidu.com/share/link?shareid=2902188921&uk=923776187
GDataXML和TFHpple配置是一样的(配置方式参考 http://blog.csdn.net/ryantang03/article/details/7868246)
主要封装代码
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:0]; if (htmlContent.count<=0) { 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==1) { 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的用法 (通用)
#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:0 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<=0) { NSLog(@"没有匹配到数据"); return Elements; } NSMutableArray *result=[NSMutableArray array]; //遍历匹配到的每个元素 for (int i=0; i<Elements.count; i++) { GDataXMLElement *element=Elements[i]; if (attributeArrarys.count>0) {//如果节点有元素 NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0]; for (int i=0; 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 第二种封装(一个子节点没有属性)推荐使用
只适合这种xml数据格式
<?xml version="1.0" encoding="utf-8"?> <Users> <User id="001"> <name>Ryan</name> <age>24</age> </User> <User id="002"> <name>Tang</name> <age>23</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:0]; keyString = [[NSMutableString alloc] initWithCapacity:0]; xmlDataSource=[[NSMutableDictionary alloc] initWithCapacity:0]; 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=0; 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=0; i<100000; i++) { if ([array containsObject:elementName]) { elementName = [NSString stringWithFormat:@"%@%d", temp, i+1]; } else { return elementName; } } return nil; } - (id)getStringByKey:(NSString*)key { NSArray *array = [key componentsSeparatedByString:@"/"]; NSDictionary *dic = [NSDictionary dictionary]; for (int i=0; i<[array count]-1; i++) { dic = [dic objectForKey:[array objectAtIndex:i]]; } NSString *temp = [dic objectForKey:[array objectAtIndex:[array count]-1]]; if (!temp) { temp = @""; } return temp; } -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ if ([dicNameArray count]>0) { 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=0; i<[dicNameArray count]-1; i++) { dic=[dic objectForKey:(NSString*)[dicNameArray objectAtIndex:i]]; } return dic; } -(void) removeLastDic { if ([dicNameArray count]>0) { [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:1]]; } NSMutableDictionary *dic = [self getLastDic]; [dic setObject:[NSString stringWithString:keyString] forKey:[dicNameArray objectAtIndex:[dicNameArray count]-1]]; } } [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:0]; } } -(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:@"1" 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:@"2" ofType:@"xml"]; NSData *data=[[[NSData alloc] initWithContentsOfFile:path] autorelease]; UseXmlParser *xmlParse=[[UseXmlParser alloc] initWithParserData:data target:self connectId:100]; [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:@"1" 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:@"2" ofType:@"xml"]; NSData *data=[[NSData alloc] initWithContentsOfFile:path]; NSArray *result= [JSDataXml ParseXmlWithPath:data XPath:@"//name" attributes:nil]; NSLog(@"__%@-->%zi",result,result.count); } @end
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频