标签:
XML解析常见的两种方式:DOM解析和SAX解析
DOM解析
解析代码如下:
-(void)xmlDOMMethed{
//盛放所有的字典
self.sourceArray = [NSMutableArray array];
//setp1.得到需要解析的数据
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"XMLDemo" ofType:@"xml"];
//step2.转换为NSData类型
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
//step3.1 构建document文档对象(options预留参数)
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
//step3.2 找到根节点(students)
GDataXMLElement *rootElement = [doc rootElement];
//step3.3 找到根节点的所有子节点
NSArray *allSubNotes = [rootElement elementsForName:@"student"];
//step3.3 找到student节点的所有子节点
for (GDataXMLElement *item in allSubNotes) {
//每次循环开始的时候,说明是一个新的student节点,我们需要字典来盛放它所有的值
NSMutableDictionary *studentDic = [NSMutableDictionary dictionary];
//得到name节点
NSArray *nameArray = [item elementsForName:@"name"];
GDataXMLElement *nameElement = [nameArray objectAtIndex:0];
NSString *name = [nameElement stringValue];
[studentDic setObject:name forKey:@"name"];
//取出age
NSArray *ageArray = [item elementsForName:@"age"];
GDataXMLElement *ageElement = [ageArray objectAtIndex:0];
NSString *age = [ageElement stringValue];
[studentDic setObject:age forKey:@"age"];
//取出sex
NSArray *sexArray = [item elementsForName:@"sex"];
GDataXMLElement *sexElement = [sexArray objectAtIndex:0];
NSString *sex = [sexElement stringValue];
[studentDic setObject:sex forKey:@"sex"];
//把student字典添加到数组中
[self.sourceArray addObject:studentDic];
}
NSLog(@"%@",self.sourceArray);
}
写入代码如下:
//通过dom解析方式为xml增加节点 (sax只可以读取,不可以添加)
- (void)domAddNote{
//获得文件路径
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"XMLDemo" ofType:@"xml"];
//将文件转换为data类型
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
GDataXMLElement *rootElement = [doc rootElement];
//创建一个我们需要添加的节点(student)
GDataXMLElement *createElement = [GDataXMLElement elementWithName:@"student"];
//为student节点添加属性
[createElement addAttribute:[GDataXMLElement attributeWithName:@"attribute" stringValue:@"aa"]];
//为student节点添加子节点
GDataXMLElement *nameNode = [GDataXMLElement elementWithName:@"name" stringValue:@"美丽"];
[createElement addChild:nameNode];
GDataXMLElement *ageNode = [GDataXMLElement elementWithName:@"age" stringValue:@"18"];
[createElement addChild:ageNode];
GDataXMLElement *sexNode = [GDataXMLElement elementWithName:@"sex" stringValue:@"男"];
[createElement addChild:sexNode];
//将创建好的student节点,添加到根节点,也就是students节点下
[rootElement addChild:createElement];
//得到所有的student节点
NSArray *stuElementArray = [rootElement elementsForName:@"student"];
//遍历得到每个student节点,以便获得student的子节点的值。
for (GDataXMLElement* item in stuElementArray) {
//获得student节点的attribute属性值
NSLog(@"%@",[[item attributeForName:@"attribute"] stringValue]);
NSArray *nameArray = [item elementsForName:@"name"];
GDataXMLElement *nameElement = [nameArray objectAtIndex:0];
NSString *name = [nameElement stringValue];
//取出age
NSArray *ageArray = [item elementsForName:@"age"];
GDataXMLElement *ageElement = [ageArray objectAtIndex:0];
NSString *age = [ageElement stringValue];
//取出sex
NSArray *sexArray = [item elementsForName:@"sex"];
GDataXMLElement *sexElement = [sexArray objectAtIndex:0];
NSString *sex = [sexElement stringValue];
NSLog(@"name=%@---age=%@----sex=%@",name,age,sex);
}
}
SAX解析
//遵循协议
@interface rootViewController ()<NSXMLParserDelegate>
//xml解析之sax解析
-(void)xmlSaxMethod{
//得到需要解析的数据
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"XMLDemo" ofType:@"xml"];
NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];
//创建sax解析的工具类对象
NSXMLParser *saxParser = [[NSXMLParser alloc] initWithData:xmlData];
//指定代理
saxParser.delegate = self;
//开始解析 sax解析是一个同步的过程
BOOL isParse = [saxParser parse];
if (isParse) {
NSLog(@"解析完成");
}else{
NSLog(@"解析失败");
}
NSLog(@"我是在解析结束下面");
}
pragma mark - sax解析的代理方法
//开始解析的代理方法
-(void)parserDidStartDocument:(NSXMLParser *)parser {
NSLog(@"开始解析");
self.saxArray = [NSMutableArray array];
}
//开始解析某个节点
//elementName:标签名称
//namespaceURI:命名空间指向的链接
//qName:命名空间的名称
//attributeDict:节点的所有属性
-(void)parser:(NSXMLParser )parser didStartElement:(NSString )elementName namespaceURI:(NSString )namespaceURI qualifiedName:(NSString )qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict {
NSLog(@"开始解析%@节点",elementName);
//当开始解析student标签的时候,就应该初始化字典,因为一个字典就对应的是一个学生的信息
if ([elementName isEqualToString:@"student"]) {
self.saxDic = [NSMutableDictionary dictionary];
}
}
//获取节点之间的值
-(void)parser:(NSXMLParser )parser foundCharacters:(NSString )string {
NSLog(@"取值--------%@",string);
if (self.valueString) {//说明有值
[self.valueString appendString:string];
} else {
self.valueString = [NSMutableString stringWithString:string];
}
}
//某个节点结束取值
-(void)parser:(NSXMLParser )parser didEndElement:(NSString )elementName namespaceURI:(NSString )namespaceURI qualifiedName:(NSString )qName {
if ([elementName isEqualToString:@"name"]) {//说明name节点已经取值结束
[self.saxDic setObject:self.valueString forKey:elementName];
}
if ([elementName isEqualToString:@"age"]) {
[self.saxDic setObject:self.valueString forKey:elementName];
}
if ([elementName isEqualToString:@"sex"]) {
[self.saxDic setObject:self.valueString forKey:elementName];
}
if ([elementName isEqualToString:@"student"]) {
[self.saxArray addObject:self.saxDic];
}
self.valueString = nil;//置空
NSLog(@"结束%@节点的解析",elementName);
}
//结束解析
-(void)parserDidEndDocument:(NSXMLParser *)parser {
//可以使用解析完成的数据
NSLog(@"%@",self.saxArray);
NSLog(@"整个解析结束");
}
//解析出错
-(void)parser:(NSXMLParser )parser parseErrorOccurred:(NSError )parseError {
NSLog(@"解析出现错误-------%@",parseError.description);
}
标签:
原文地址:http://blog.csdn.net/u010390827/article/details/51334784