iOS开发中使用GData处理简单结构的XML非常简单,只需要明白GData提供的几个方法意思就可以。这里做一下简单的示例:
首先读取原xml数据到系统中
// 原xml结构示例
// <m>
// <c>2000101</c>
// <n_1>一般职业</n_1>
// <n_2>机关</n_2>
// <n_3>机关团体内勤人员</n_3>
// <sx>1</sx>
// <yw>1</yw>
// <yl>1</yl>
// <hm>1</hm>
// </m>
// 获取工程目录的xml文件
NSData * xmlData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"010" ofType:@"xml"]];
// 从文档中读出完整的XML数据,在内存中形成完整的树形结构
NSError * error = nil;
GDataXMLDocument * documents = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:&error];
// 取得根节点(element元素)
GDataXMLElement * rootEL = [documents rootElement];
// 获取根节点下的节点,返回的数组
NSArray * studentsArray = [rootEL elementsForName:@"m"];
for (int i = 0; i < [studentsArray count]; i++) {
// 获取单个节点对象
GDataXMLElement * student = [studentsArray objectAtIndex:i];
// 获取m节点下节点的值
GDataXMLElement * code = [[student elementsForName:@"c"] lastObject];
GDataXMLElement * n_1 = [[student elementsForName:@"n_1"]lastObject];
GDataXMLElement * n_2 = [[student elementsForName:@"n_2"]lastObject];
GDataXMLElement * n_3 = [[student elementsForName:@"n_3"]lastObject];
GDataXMLElement * sx = [[student elementsForName:@"sx"] lastObject];
GDataXMLElement * yw = [[student elementsForName:@"yw"]lastObject];
GDataXMLElement * yl = [[student elementsForName:@"yl"]lastObject];
GDataXMLElement * hm = [[student elementsForName:@"hm"]lastObject];
NSString * strcode = [code stringValue];
NSString * strn_1 = [n_1 stringValue];
NSString * strn_2 = [n_2 stringValue];
NSString * strn_3 = [n_3 stringValue];
NSString * strsx = [sx stringValue];
NSString * stryw = [yw stringValue];
NSString * stryl = [yl stringValue];
NSString * strhm = [hm stringValue];
ZSXMLModel *model = [[ZSXMLModel alloc]init];
model.code = strcode;
model.n_1 = strn_1;
model.n_2 = strn_2;
model.n_3 = strn_3;
model.sx = strsx;
model.yw = stryw;
model.yl = stryl;
model.hm = strhm;
[_mArr addObject:model];
// 读标签里面的属性(attributeForName)
NSString * sign = [[student attributeForName:@"sign"]stringValue];
if (sign) {
NSLog(@"----------%@",sign);
}
下面处理读取的xml数据
// 目标结构
// <?xml version="1.0" encoding="utf-8"?>
// <root>
// <c1 id="2000000" name="一般职业">
// <c2 id="2000100" name="机关">
// <c3 id="2000101" name="机关团体外勤人员(从事联系工作)" sx="1" yw="1" yl="1" hm="1" ></c3>
// <c3 id="2000102" name="机关团体外勤人员(从事联系工作)1" sx="1" yw="1" yl="1" hm="1" ></c3>
// <c3 id="2000103" name="机关团体外勤人员(从事联系工作)2" sx="1" yw="1" yl="1" hm="1" ></c3>
// </c2>
// <c2 id="2000200" name="工厂">
// <c3 id="2000201" name="机关团体内勤人员" sx="1" yw="1" yl="1" hm="1" ></c3>
// <c3 id="2000202" name="机关团体内勤人员" sx="1" yw="1" yl="1" hm="1" ></c3>
// <c3 id="2000203" name="机关团体内勤人员" sx="1" yw="1" yl="1" hm="1" ></c3>
// </c2>
// </c1>
// </root>
//生成根节点
GDataXMLElement *root = [GDataXMLElement elementWithName:@"root"];
GDataXMLElement *c2 = [GDataXMLElement elementWithName:@"c2"];
GDataXMLElement *c1 = [GDataXMLElement elementWithName:@"c1"];
for (int i = 0; i < _mArr.count; i++) {
ZSXMLModel *model = _mArr[i];
ZSXMLModel *modeln = _mArr[i == _mArr.count - 1 ? i : i+1];
//c1
NSString *strC1c = [NSString stringWithFormat:@"%@0000",[model.code substringWithRange:NSMakeRange(0, 3)]];
GDataXMLElement *c1id = [GDataXMLElement attributeWithName:@"id" stringValue:strC1c];
GDataXMLElement *c1n = [GDataXMLElement attributeWithName:@"name" stringValue:model.n_1];
[c1 addAttribute:c1id];
[c1 addAttribute:c1n];
//c2
NSString *strC2c = [NSString stringWithFormat:@"%@00",[model.code substringWithRange:NSMakeRange(0, 5)]];
GDataXMLElement *c2id = [GDataXMLElement attributeWithName:@"id" stringValue:strC2c];
GDataXMLElement *c2n = [GDataXMLElement attributeWithName:@"name" stringValue:model.n_2];
[c2 addAttribute:c2id];
[c2 addAttribute:c2n];
//c3
GDataXMLElement *c3 = [GDataXMLElement elementWithName:@"c3"];
GDataXMLElement *c3id = [GDataXMLElement attributeWithName:@"id" stringValue:model.code];
GDataXMLElement *c3n = [GDataXMLElement attributeWithName:@"name" stringValue:model.n_3];
GDataXMLElement *sx = [GDataXMLElement attributeWithName:@"sx" stringValue:model.sx];
GDataXMLElement *yw = [GDataXMLElement attributeWithName:@"yw" stringValue:model.yw];
GDataXMLElement *yl = [GDataXMLElement attributeWithName:@"yl" stringValue:model.yl];
GDataXMLElement *hm = [GDataXMLElement attributeWithName:@"hm" stringValue:model.hm];
[c3 addAttribute:c3id];
[c3 addAttribute:c3n];
[c3 addAttribute:sx];
[c3 addAttribute:yw];
[c3 addAttribute:yl];
[c3 addAttribute:hm];
[c2 addChild:c3];
if (![model.n_2 isEqualToString:modeln.n_2] || i == _mArr.count-1) {
[c1 addChild:c2];
if (i != _mArr.count-1)
c2 = [GDataXMLElement elementWithName:@"c2"];
}
if (![model.n_1 isEqualToString:modeln.n_1] || i == _mArr.count-1) {
[root addChild:c1];
if (i != _mArr.count-1)
c1 = [GDataXMLElement elementWithName:@"c1"];
}
}
GDataXMLDocument *xmlDoc = [[GDataXMLDocument alloc] initWithRootElement:root];
//【关键】防止乱码
[xmlDoc setCharacterEncoding:@"utf-8"];
NSData *data1 = [xmlDoc XMLData];
NSString *xmlString = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(@"%@", xmlString);
注意:
将子节点添加到父节点后子节点就不能再进行编辑,所以需要将子节点的所有数据都加入到该节点后,再把子节点添加到父节点中。