系统自带了两个解析xml 的库,前面已经写过NSXMLParser 的用法,今天整理一下libxml2的解析用法
首先要包含libxml2的类库,并添加如下信息,见图
见实例代码:
#import <UIKit/UIKit.h>
@class OderListModel;
@class OrderListDetailModel;
@interface ViewController : UIViewController
{
OderListModel* oderListModel;
OrderListDetailModel* orderDetailModel;
}
@property(nonatomic,strong)NSData* nsData;
@end
#import "ViewController.h"
#import <libxml/xmlreader.h>
#import "ASIHTTPRequest.h"//第三方库,异步请求数据
#import "OderListModel.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
oderListModel=[[OderListModel alloc] init];
oderListModel.dataList=[[NSMutableArray alloc] init];
orderDetailModel=[[OrderListDetailModel alloc] init];
// Do any additional setup after loading the view, typically from a nib.
//[self xmlParse];
[self requetT];
}
-(void)requetT
{
NSURL* requestUrl=[NSURL URLWithString:@""];
ASIHTTPRequest* request=[ASIHTTPRequest requestWithURL:requestUrl];
request.delegate=self;
[request startAsynchronous];//异步请求
//[request setRequestMethod:@"get"];
}
-(void)requestFinished:(ASIHTTPRequest*)request
{
self.nsData=[request responseData];//以data形式,取出下载的数据
xmlTextReaderPtr reader=xmlReaderForMemory([self.nsData bytes], [self.nsData length], nil, nil, 0);
if (!reader) {
NSLog(@"Failed to load xmlReader");
}
NSString* currentTagName=nil;
NSString* currenttagValue=nil;
NSString* parentTagName=nil;
BOOL itemStarted=false;
char* temp;
// while (xmlTextReaderRead(reader) ) {
// NSLog(@"type:%d",xmlTextReaderNodeType(reader));
// temp=(char*)xmlTextReaderConstName(reader);
// if (temp) {
// NSLog(@"typeTag:%@",currentTagName=[NSString stringWithCString:temp encoding:NSUTF8StringEncoding]);
// }
// temp=(char*)xmlTextReaderConstValue(reader);
// if (temp) {
// NSLog(@"typeValue:%@",[NSString stringWithCString:temp encoding:NSUTF8StringEncoding]);
// }
//
// }
while (true) {
if (!xmlTextReaderRead(reader)) {
break;
}
int type=xmlTextReaderNodeType(reader);
//NSLog(@"type")
switch (type) {
case XML_READER_TYPE_ELEMENT://开始元素节点
temp=(char*)xmlTextReaderConstName(reader);
currentTagName=[NSString stringWithCString:temp encoding:NSUTF8StringEncoding];
if ([currentTagName isEqualToString:@"root"]||[currentTagName isEqualToString:@"order"]) {
parentTagName=currentTagName;
itemStarted=YES;
}
continue;
case XML_READER_TYPE_TEXT://值元素节点
temp=(char*)xmlTextReaderConstValue(reader);
if (!itemStarted) {
continue;
}
currenttagValue=[NSString stringWithCString:temp encoding:NSUTF8StringEncoding];
if ([parentTagName isEqualToString:@"root"]) {
if ([currentTagName isEqualToString:@"issuccess"]) {
oderListModel.issuccess=currenttagValue;
}
else if ([currentTagName isEqualToString:@"errormessage"])
{
oderListModel.errormessage=currenttagValue;
}
else if ([currentTagName isEqualToString:@"totalcount"])
{
oderListModel.totalcount=currenttagValue;
}
}else if([parentTagName isEqualToString:@"order"])
{
if([currentTagName isEqualToString:@"orderid"])
{
orderDetailModel.orderid=currenttagValue;
}
else if ([currentTagName isEqualToString:@"yzsoufunname"])
{
orderDetailModel.yzsoufunname=currenttagValue;
}
else if([currentTagName isEqualToString:@"ownername"])
{
orderDetailModel.ownername=currenttagValue;
}
else if ([currentTagName isEqualToString:@"ownersoufunid"])
{
orderDetailModel.ownersoufunid=currenttagValue;
}
else if ([currentTagName isEqualToString:@"estateid"])
{
orderDetailModel.estateid=currenttagValue;
}
else if ([currentTagName isEqualToString:@"estatename"])
{
orderDetailModel.estatename=currenttagValue;
}
else if ([currentTagName isEqualToString:@"stagemoney"])
{
orderDetailModel.stagemoney=currenttagValue;
}
else if ([currentTagName isEqualToString:@"createtime"])
{
orderDetailModel.createtime=currenttagValue;
}
}
continue;
case XML_READER_TYPE_END_ELEMENT://结束元素节点
temp=(char*)xmlTextReaderConstName(reader);
currentTagName=[NSString stringWithCString:temp encoding:NSUTF8StringEncoding];
if ([currentTagName isEqualToString:@"root"]) {
[self operateCompUser:oderListModel];
}else if ([currentTagName isEqualToString:@"order"])
{
[oderListModel.dataList addObject:orderDetailModel];
}
continue;
default:
break;
}
}
}
-(void)operateCompUser:(OderListModel*)oderListmodel
{
NSLog(@"isSuccess:%@",oderListModel.issuccess);
NSLog(@"ErroMessage:%@",oderListModel.errormessage);
NSLog(@"totalCount:%@",oderListModel.totalcount);
NSLog(@"dataList:%@",oderListModel.dataList);
}
-(void)xmlParse
{
// NSFileManager* fm=[NSFileManager defaultManager];
//
//
// NSString* path=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
// BOOL iss=[fm fileExistsAtPath:path];
// // NSString* ss=@"as";
// xmlTextReaderPtr reader = xmlNewTextReaderFilename([path UTF8String]);
// int ret= xmlTextReaderRead(reader);//1表示成功读取,-1文件内容为空
// if (ret==0) {//到文件尾部
// return;
// }
// else if(ret!=1)//
// {
// return;
// }
// xmlChar* element= xmlTextReaderName(reader);//
// if (element!= NULL) {
// int ntype=xmlTextReaderNodeType(reader);//读取元素的类型
// if (strcmp((const char*)element, "param-name")==0) {
// xmlFree(element);
// if (XML_READER_TYPE_ELEMENT==ntype) {
// int a=1;
// }
// }
// }
// self.nsData=[theRequest respo]
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
model文件
#import <Foundation/Foundation.h>
@interface OderListModel : NSObject
@property(nonatomic,copy)NSString* issuccess;
@property(nonatomic,copy)NSString* errormessage;
@property(nonatomic,copy)NSString* totalcount;
@property(nonatomic,strong)NSMutableArray* dataList;
@end
@interface OrderListDetailModel : NSObject
@property(nonatomic,copy)NSString* orderid;
@property(nonatomic,copy)NSString* yzsoufunname;
@property(nonatomic,copy)NSString* ownername;
@property(nonatomic,copy)NSString* ownersoufunid;
@property(nonatomic,copy)NSString* estateid;
@property(nonatomic,copy)NSString* estatename;
@property(nonatomic,copy)NSString* stagemoney;
@property(nonatomic,copy)NSString* createtime;
@end
根据类型来进行判断是开始节点、注释节点、值节点、结束节点所有的节点类型如下
typedef enum {
XML_READER_TYPE_NONE = 0,//节点类型
XML_READER_TYPE_ELEMENT = 1,//元素节点类型
XML_READER_TYPE_ATTRIBUTE = 2,//
XML_READER_TYPE_TEXT = 3,//值节点类型
XML_READER_TYPE_CDATA = 4,//CDATA值节点类型
XML_READER_TYPE_ENTITY_REFERENCE = 5,
XML_READER_TYPE_ENTITY = 6,
XML_READER_TYPE_PROCESSING_INSTRUCTION = 7,
XML_READER_TYPE_COMMENT = 8,//注释节点类型
XML_READER_TYPE_DOCUMENT = 9,
XML_READER_TYPE_DOCUMENT_TYPE = 10,
XML_READER_TYPE_DOCUMENT_FRAGMENT = 11,
XML_READER_TYPE_NOTATION = 12,
XML_READER_TYPE_WHITESPACE = 13,
XML_READER_TYPE_SIGNIFICANT_WHITESPACE = 14,//
XML_READER_TYPE_END_ELEMENT = 15,//结束元素节点类型
XML_READER_TYPE_END_ENTITY = 16,
XML_READER_TYPE_XML_DECLARATION = 17
} xmlReaderTypes;