xml解析例子

前端之家收集整理的这篇文章主要介绍了xml解析例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<?xml version = "1.0" encoding = "UTF8" standalone = "yes" ?>

<!--上面的是预处理指令 编码方式为UTF8 版本为1.0 standalone 说明他是独立的 与其他没有关系-->

<!--以下是一个学生名单-->

<student_list>

<student>

<stu_name>otis</stu_name>

<stu_class>T23</stu_class>

<stu_num>2003081205</stu_num>

</student>

<stu_name>tim</stu_name>

<stu_num>2003081210</stu_num>

<stu_name>lili</stu_name>

<stu_class>T24</stu_class>

<stu_num>2003081206</stu_num>

</student_list>

//以上是xml的内容,我把它存在一个名为 text.xml的文本中



viewcontroller.h文件

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<NSXMLParserDelegate>


@property (strong,nonatomic)IBOutlet UITextView *m_pTextViewResult;

)NSXMLParser *m_pXmlParse;//系统带得 解析的类 即:解析器

)NSMutableString *m_pStrSaxResult;//存解析结果

)NSMutableString *m_pStrSaxTmp;//存解析的临时结果


-(IBAction)btnClickXMLParSEOfSAX:(id)sender;


@end


。m文件

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

[super viewDidLoad];

@H_301_243@ // Do any additional setup after loading the view,typically from a nib.

}


- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

@H_301_243@ // Dispose of any resources that can be recreated.

}


//按钮关联方法 开始解析

-(IBAction)btnClickXMLParSEOfSAX:(id)sender

{

NSLog(@"%s",__func__);


@H_301_243@ //*******加载文件

@H_301_243@ //获取text 路径

NSString *path = [[NSBundle mainBundle]pathForResource:@"text" ofType:@"xml"];

//定义一个文件句柄关联路径

NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path];//读取

@H_301_243@ //读取数据到data

NSData *data = [file readDataToEndOfFile];

@H_301_243@ //关闭文件

[file closeFile];

@H_301_243@ //根据xml数据进行创建解析器 初始化 .m_pXmlParse

self.m_pXmlParse = [[NSXMLParser alloc]initWithData:data];

@H_301_243@ //设定代理

@H_301_243@ [self.m_pXmlParse setDelegate:self];//解析器与当前视图建立委托

self.m_pStrSaxResult = [[NSMutableString alloc]init];

self.m_pStrSaxTmp = [[NSMutableString alloc]init];

//通知解析器开始解析,返回一个bool值,标志解析成功或失败

BOOL flag = [self.m_pXmlParse parse];

if (flag) {

[_m_pStrSaxResult appendFormat:@"%@\r\n",@"获取指定路径的 xml 文件成功"];

}

else

{

获取指定路径的 xml 文件失败"];


}

}


//开始解析

-(void)parserDidStartDocument:(NSXMLParser *)parser

{

NSLog(@"%s",__func__);

[_m_pStrSaxResult appendFormat:@"%@\r\n",@"开始解析 xml of SAX 文件"];

];

}

//遇到数据时回调,包括空格和回车

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

{

NSLog(@"%s",__func__);


[_m_pStrSaxTmp setString:@""];

[_m_pStrSaxTmp appendFormat:@"%@",string];

NSLog(@"string = %@",string);

}


//当遇到结束标签调用

-(void)parser:(NSXMLParser *)parser

didEndElement:(NSString *)elementName //之处理 end 标签

namespaceURI:(NSString *)namespaceURL

qualifiedName:(NSString *)qName


{

NSLog(@"%s",__func__);


if (NSOrderedSame == [elementName compare:@"student"]) {

[_m_pStrSaxResult appendFormat:@"%@\n",@"*******student*********"];

}

else if (NSOrderedSame == [elementName compare:@"student_list"])

{

}

else

{

[_m_pStrSaxResult appendFormat:@"%@ = %@\r\n",elementName,_m_pStrSaxTmp];

}

}

@H_301_243@ //完成解析xml 文档

-(void)parserDidEndDocument:(NSXMLParser *)parser

{

NSLog(@"%s",__func__);


[_m_pStrSaxResult appendFormat:@"%@",@"解析 xml 文件完成"];

[_m_pTextViewResult setText:_m_pStrSaxResult];

}


@end

原文链接:https://www.f2er.com/xml/299173.html

猜你在找的XML相关文章