解析以下文档:使用 xml
Student.xml
<students>
<student>
<name>张三</name>
<age>18</age>
<gender>男</gender>
</student>
<student>
<name>李四</name>
<age>28</age>
<gender>男</gender>
</student>
<student>
<name>王五</name>
<age>38</age>
<gender>女</gender>
</student>
</students>
首先创建 student 类.
//student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic, strong) NSString *name,*age,*gender;
@end
//student.m
#import "Student.h"
@implementation Student
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@,gender:%@,age:%@", self.name,self.gender,self.age];
}
@end
使用storyboard可视化手动拉去一个按钮
例如图:
- 导入libxml2.tbd库
#import "ViewController.h"
#import "Student.h"
//导入 xml 解析时要用到的协议NSXMLParserDelegate
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, retain) NSMutableArray *studentArray;
@property (nonatomic, retain) NSString *string;
@property (nonatomic, strong) Student *stu;
@end
- xml 解析之 sax 解析
- (IBAction)parserWithSAX:(id)sender {
//找到资源路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];
//创建 URL
NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
// 根据 URL 创建解析类
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//设置代理,xml 的解析是逐行进行解析的,但是它的解析方法在 delegate 中,所以要遵守协议.设置代理
parser.delegate = self;
//开始解析
[parser parse];
}
//实现协议方法
#pragma mark----NSXMLParserDelegate
//开始解析
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"开始解析文档");
self.studentArray = [NSMutableArray arrayWithCapacity:0];
}
//结束解析
- (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"已经结束解析文档");
for (Student *student in self.studentArray) {
NSLog(@"%@",student);
}
}
//解析过程中遇到开始解析的标题
//开始解析某个节点
//elementName标签名称
//namespaceURI 命名空间指向的链接.
//qualifiedName:命名空间的名称
//attributes:节点所有属性
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
NSLog(@"遇到开始标签:%@",elementName);
if ([elementName isEqualToString:@"student"]) {
self.stu = [[Student alloc] init];
}
}
//在解析过程中遇到结束的标题
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"遇到结束标签:%@",elementName);
if ([elementName isEqualToString:@"name"]) {
self.stu.name = self.string;
}else if ([elementName isEqualToString:@"gender"]){
self.stu.gender = self.string;
}else if ([elementName isEqualToString:@"age"]){
self.stu.age = self.string;
}else if ([elementName isEqualToString:@"student"]){
[self.studentArray addObject:self.stu];//如果
}
}
//在解析的过程中遇到需要解析出来的字符
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"遇到解析的字符串:%@",string);
self.string = string;
}
解析出的结果为:
2016-07-03 18:26:56.420 XML[1952:149358] 开始解析文档
2016-07-03 18:26:56.420 XML[1952:149358] 遇到开始标签:students
2016-07-03 18:26:56.420 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.420 XML[1952:149358] 遇到开始标签:student
2016-07-03 18:26:56.420 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.420 XML[1952:149358] 遇到开始标签:name
2016-07-03 18:26:56.420 XML[1952:149358] 遇到解析的字符串:张三
2016-07-03 18:26:56.421 XML[1952:149358] 遇到结束标签:name
2016-07-03 18:26:56.421 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.421 XML[1952:149358] 遇到开始标签:age
2016-07-03 18:26:56.421 XML[1952:149358] 遇到解析的字符串:18
2016-07-03 18:26:56.421 XML[1952:149358] 遇到结束标签:age
2016-07-03 18:26:56.421 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.421 XML[1952:149358] 遇到开始标签:gender
2016-07-03 18:26:56.421 XML[1952:149358] 遇到解析的字符串:男
2016-07-03 18:26:56.421 XML[1952:149358] 遇到结束标签:gender
2016-07-03 18:26:56.422 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.422 XML[1952:149358] 遇到结束标签:student
2016-07-03 18:26:56.422 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.422 XML[1952:149358] 遇到开始标签:student
2016-07-03 18:26:56.422 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.422 XML[1952:149358] 遇到开始标签:name
2016-07-03 18:26:56.422 XML[1952:149358] 遇到解析的字符串:李四
2016-07-03 18:26:56.422 XML[1952:149358] 遇到结束标签:name
2016-07-03 18:26:56.422 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.423 XML[1952:149358] 遇到开始标签:age
2016-07-03 18:26:56.423 XML[1952:149358] 遇到解析的字符串:28
2016-07-03 18:26:56.423 XML[1952:149358] 遇到结束标签:age
2016-07-03 18:26:56.423 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.423 XML[1952:149358] 遇到开始标签:gender
2016-07-03 18:26:56.423 XML[1952:149358] 遇到解析的字符串:男
2016-07-03 18:26:56.423 XML[1952:149358] 遇到结束标签:gender
2016-07-03 18:26:56.423 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.423 XML[1952:149358] 遇到结束标签:student
2016-07-03 18:26:56.424 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.424 XML[1952:149358] 遇到开始标签:student
2016-07-03 18:26:56.424 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.424 XML[1952:149358] 遇到开始标签:name
2016-07-03 18:26:56.424 XML[1952:149358] 遇到解析的字符串:王五
2016-07-03 18:26:56.424 XML[1952:149358] 遇到结束标签:name
2016-07-03 18:26:56.424 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.424 XML[1952:149358] 遇到开始标签:age
2016-07-03 18:26:56.424 XML[1952:149358] 遇到解析的字符串:38
2016-07-03 18:26:56.425 XML[1952:149358] 遇到结束标签:age
2016-07-03 18:26:56.425 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.425 XML[1952:149358] 遇到开始标签:gender
2016-07-03 18:26:56.425 XML[1952:149358] 遇到解析的字符串:女
2016-07-03 18:26:56.425 XML[1952:149358] 遇到结束标签:gender
2016-07-03 18:26:56.425 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.425 XML[1952:149358] 遇到结束标签:student
2016-07-03 18:26:56.425 XML[1952:149358] 遇到解析的字符串:
2016-07-03 18:26:56.425 XML[1952:149358] 遇到结束标签:students
2016-07-03 18:26:56.426 XML[1952:149358] 已经结束解析文档
2016-07-03 18:26:56.426 XML[1952:149358] name:张三,gender:男,age:18
2016-07-03 18:26:56.426 XML[1952:149358] name:李四,gender:男,age:28
2016-07-03 18:26:56.426 XML[1952:149358] name:王五,gender:女,age:38
- XML 解析值 DMO解析
同样在可视化界面里面拉去按钮,设置成DMO 解析
#import "GDataXMLNode.h"
要下载一对文件 GDataXMLNode 的. h 和. m 文件
- (IBAction)parserWithDOM:(id)sender {
//获取文件的路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];
NSError *error = nil;
//获取内容
NSString *content = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
NSLog(@"%@",content);
}else{
NSLog(@"%@",error);
return;
}
// 创建GDataXMLDocument对象.此时的 xml 文件内所有的节点以树的形式存在GDataXMLDocument中.
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:content options:0 error:nil];
//获取根节点,在根节点里包含了xml所有的信息.
GDataXMLElement *rootElement = [document rootElement];//取到根节点
//查找子节点,并获取.
NSArray *studentElements = [rootElement elementsForName:@"student"];
NSLog(@"%@",studentElements);
self.studentArray = [NSMutableArray arrayWithCapacity:3];
for (GDataXMLElement *element in studentElements) {
Student *student = [[Student alloc] init];
student.name = [[[element elementsForName:@"name"] firstObject] stringValue];
student.age =[[[element elementsForName:@"age"] firstObject] stringValue];
student.gender = [[[element elementsForName:@"gender"] firstObject] stringValue];
[self.studentArray addObject:student];
}
for (Student *stu in self.studentArray) {
NSLog(@"%@",stu);
}
}
解析出的结果为
2016-07-03 18:42:00.845 XML[1952:149358] <students>
<student>
<name>张三</name>
<age>18</age>
<gender>男</gender>
</student>
<student>
<name>李四</name>
<age>28</age>
<gender>男</gender>
</student>
<student>
<name>王五</name>
<age>38</age>
<gender>女</gender>
</student>
</students>
2016-07-03 18:42:00.846 XML[1952:149358] (
"GDataXMLElement 0x7ff6d85958c0: {type:1 name:student xml:\"<student><name>\U5f20\U4e09</name><age>18</age><gender>\U7537</gender></student>\"}",
"GDataXMLElement 0x7ff6d851c450: {type:1 name:student xml:\"<student><name>\U674e\U56db</name><age>28</age><gender>\U7537</gender></student>\"}",
"GDataXMLElement 0x7ff6d851c4c0: {type:1 name:student xml:\"<student><name>\U738b\U4e94</name><age>38</age><gender>\U5973</gender></student>\"}"
)
2016-07-03 18:42:00.846 XML[1952:149358] name:张三,gender:男,age:18
2016-07-03 18:42:00.846 XML[1952:149358] name:李四,gender:男,age:28
2016-07-03 18:42:00.846 XML[1952:149358] name:王五,gender:女,age:38
网友评论