#import "Student.h"
#import "GdataxMLNode.h"
@interface ViewController ()<NSXMLParserDelegate>
{
NSString *_recordElementName;
}
@property (nonatomic,strong)NSMutableArray *XMLDomArray;
@property (nonatomic,strong)NSMutableArray *XmlSAXArray;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 注意使用
1、找到“Paths\Header Search Paths”项,并添加“/usr/include/libxml2”到列表中
2、找到“Linking\Other Linker Flags”项,并添加“-lxml2”到列表中
3、需要在Build Phases->CompileSorces ->双击GdataxMLNode.m,添加-fno-objc-arc
[self XMLDom];
[self XMLSax];
}
#pragma mark - XML的DOM解析
- (void)XMLDom{
_XMLDomArray = [[NSMutableArray alloc]init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"xml"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSError *error = nil;
GdataxMLDocument *document = [[GdataxMLDocument alloc]initWithData:data options:0 error:&error];
// 取出xml的根节点
GdataxMLElement *root = document.rootElement;
// 取出xml根节点下的所有子节点
NSArray *rootChile = root.children;
for (GdataxMLElement *child in rootChile) {
GdataxMLElement *nameElement = [[child elementsForName:@"name"]objectAtIndex:0];
NSString *name = [nameElement stringValue];
NSLog(@"%@",name);
GdataxMLElement *ageElement = [[child elementsForName:@"age"]objectAtIndex:0];
NSString *age = [ageElement stringValue];
NSLog(@"%@",age);
}
}
#pragma mark - XML的SAX解析
- (void)XMLSax{
NSString *path = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@".xml"];
// 将数据转换成二进制的nsdata
NSData *data = [NSData dataWithContentsOfFile:path];
// 系统提供的一个雷用来解析nsdata
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
// 相关属性
// 设置是否处理命名空间的(版权问题)
[parser setShouldProcessNamespaces:NO];
// 设置是否回报空间的处理结果
[parser setShouldReportNamespacePrefixes:NO];
parser.delegate = self;
// 开始解析
[parser parse];
// 代理方法
}
#pragma mark-----打开文档
-(void)parserDidStartDocument:(NSXMLParser *)parser{
// 打开文档只执行一次
self.XmlSAXArray = [NSMutableArray array];
}
#pragma mark------打开标签
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
_recordElementName = elementName;
if ([_recordElementName isEqualToString:@"student"]) {
Student *stu = [[Student alloc]init];
[self.XmlSAXArray addObject:stu];
}
}
#pragma mark-----取值
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
// 每次都是先打开标签创建模型存入数组,在取值,每次去的都是最后一个值
Student *stu = self.XmlSAXArray.lastObject;
if ([_recordElementName isEqualToString:@"name"]) {
stu.name = string;
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
_recordElementName = nil;
//XML的SAX解析方式是 打开标签——》取值——》关闭标签——》取值
// 通过声明一个标签的实例变量 进行判断,防止将以前的数据覆盖
}
#pragma mark-----关闭文档
-(void)parserDidEndDocument:(NSXMLParser *)parser{
for (Student *stu in self.XmlSAXArray) {
NSLog(@"%@",stu);
}
}
#pragma mark - xml文档
<Students>
<student>
<name>张三</name>
</student>
<student>
<name>李四</name>
</student>
#pragma mark-简历Student类
.h
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,copy)NSString *age;
@end
<student>
<name>王二</name>
</student>
</Students>
原文链接:https://www.f2er.com/xml/295072.html