这里使用的是 KissXML 第三方库生成
1,使用pod导入 KissXML
注:我使用的 Xcode 版本是 9.3.1 ,导入 KissXML 5.2.2 版本后,swift 报错,没找到原因,就直接把 KissXML 中的swift文件删除了,并不影响工程
2,生产 XML 文件,这里是用了字典作为原始数据,也可以使用数组或者字符串,但是我感觉字典是最方便的,方便查看层次和读取
原始数据(一个字典数据,如果有需要可以自行修改)
{
name = sheet;
"root_score" = 10;
sectionArray = (
{
"box_modify" = "0,1";
"box_name" = start;
"box_show" = "0,1,2,3,5,6,1001,1002,1003,1005,1006";
"box_title" = "\U5de5\U5355\U53d1\U8d77";
rowArray = (
{
"child_name" = attention;
label = "\U6ce8\U610f\U4e8b\U9879";
length = 1;
must = 1;
text = pp2121212;
type = textedit;
},
{
"child_name" = planbegin;
label = "\U8ba1\U5212\U5f00\U59cb\U65f6\U95f4";
length = 3;
type = datetime;
},
{
"child_name" = describe;
label = "\U5de5\U5355\U63cf\U8ff0";
length = 1;
must = 1;
type = textedit;
},
{
"child_name" = code;
label = "\U7f16\U53f7";
must = 2;
readonly = y;
type = textedit;
},
{
"child_name" = lastcode;
label = "\U4e0a\U4e2a\U5de5\U5355\U7f16\U53f7";
type = textview;
},
{
"child_name" = targetplace;
label = "\U76ee\U7684\U5730";
length = 1;
must = 2;
type = textedit;
},
{
"child_name" = date;
label = "\U53d1\U8d77\U65e5\U671f";
must = 2;
readonly = y1;
type = datetime;
},
{
"child_name" = planend;
label = "\U8ba1\U5212\U622a\U6b62\U65f6\U95f4";
length = 1;
must = 2;
type = datetime;
},
{
"child_name" = sender;
label = "\U53d1\U8d77\U4eba";
must = 2;
readonly = y;
type = textedit;
},
{
"child_name" = job;
defaults = "\U6d4b\U8bd5\U5c97\U4f4d1,\U6d4b\U8bd5\U5c97\U4f4d2";
label = "\U53d1\U8d77\U5c97\U4f4d";
length = 0;
must = 2;
type = selector;
}
);
},
{
"box_modify" = 3;
"box_name" = end;
"box_show" = "3,5,6,1003,1005,1006";
"box_title" = "\U5de5\U5355\U8bc4\U4ef7";
rowArray = (
{
"child_name" = score;
defaults = 100;
label = "\U5de5\U5355\U8bc4\U5206";
max = 100;
min = 0;
must = 2;
type = number;
},
{
"child_name" = comment;
label = "\U8bc4\U4ef7\U5185\U5bb9";
length = 1;
type = textedit;
}
);
},
{
"box_modify" = 1002;
"box_name" = fj;
"box_show" = "2,3,5,6,1002,1003,1005,1006";
"box_title" = "\U9644\U4ef6";
rowArray = (
{
amount = 3;
"child_name" = zp3;
label = "\U7167\U7247\U6216\U89c6\U9891";
type = media;
},
{
amount = 2;
"child_name" = zp1;
label = "\U7167\U7247";
length = 1;
type = media;
},
{
"child_name" = dtgj;
label = "\U8f68\U8ff9";
length = 2;
type = mapview;
},
{
"child_name" = jwd2;
label = "\U5f53\U524d\U4f4d\U7f6e";
location = 2;
type = textedit;
},
{
"child_name" = dtwz;
label = "\U5b9a\U4f4d";
length = 1;
type = mapview;
},
{
"child_name" = sfwc;
label = "\U662f\U5426\U5b8c\U6210";
type = bool;
},
{
amount = 2;
"child_name" = zp2;
label = "\U89c6\U9891";
length = 2;
type = media;
},
{
"child_name" = jwd1;
label = "\U7ecf\U7eac\U5ea6";
location = 1;
type = textedit;
},
{
"child_name" = jtnr;
label = "\U5177\U4f53\U5185\U5bb9";
length = 1;
type = button;
}
);
}
);
}
代码生成
//生成节点,这里作为跟节点,
DDXMLElement * xmlName = [DDXMLElement elementWithName:xmlDic[@"name"]];
for (NSString * keyStr in xmlDic.allKeys) {
if (![keyStr isEqualToString:@"name"] && ![keyStr isEqualToString:@"sectionArray"]) {
//在节点中添加属性(这里去除了两个不需要的属性)
DDXMLNode * xmlID = [DDXMLNode attributeWithName:keyStr stringValue:xmlDic[keyStr]];
//添加属性
[xmlName addAttribute:xmlID];
}
}
NSArray * sectionArray = xmlDic[@"sectionArray"];
//通过数组,添加多个一级子节点
[sectionArray enumerateObjectsUsingBlock:^(NSDictionary * obj, NSUInteger idx, BOOL * _Nonnull stop) {
//创建子节点
DDXMLElement * start = [DDXMLElement elementWithName:obj[@"box_name"]];
for (NSString * keyStr in obj.allKeys) {
//向子节点中添加熟悉(这里去除了两个不需要的属性)
if (![keyStr isEqualToString:@"box_name"] && ![keyStr isEqualToString:@"rowArray"]) {
DDXMLNode * xmlID = [DDXMLNode attributeWithName:keyStr stringValue:xmlDic[keyStr]];
//添加属性
[start addAttribute:xmlID];
}
}
//向每个一级节点中添加多个不同的二级节点
NSArray * rowArray = obj[@"rowArray"];
[rowArray enumerateObjectsUsingBlock:^(NSDictionary * objRow, NSUInteger idx, BOOL * _Nonnull stop) {
//创建二级节点以及二级节点的值
DDXMLElement * rowText = [DDXMLElement elementWithName:objRow[@"child_name"] stringValue:objRow[@"text"]];
for (NSString * kStr in objRow.allKeys) {
//这里是向二级节点添加属性(这里去除了两个不需要的属性)
if (![kStr isEqualToString:@"child_name"] && ![kStr isEqualToString:@"text"]) {
DDXMLNode * xmlI = [DDXMLNode attributeWithName:kStr stringValue:objRow[kStr]];
//添加属性
[rowText addAttribute:xmlI];
}
}
//向一级节点里面添加二级节点
[start addChild:rowText];
}];
//向跟节点里面添加一级节点
[xmlName addChild:start];
}];
//将创建的XML数据转为文件并保存到沙盒中
DDXMLDocument *peopleDocument = [[DDXMLDocument alloc] initWithXMLString:xmlName.XMLString options:0 error:nil];
NSString *path = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"people01.xml"];
[[peopleDocument XMLData] writeToFile:path atomically:YES];
DBLog(@"XML路径-++++++++%@",path);
生成的XML文件为
<?xml version="1.0" encoding="utf-8"?>
<sheet root_score="10">
<start box_title="" box_modify="" box_show="">
<attention label="注意事项" length="1" must="1" type="textedit">099999999999999</attention>
<planbegin label="计划开始时间" length="3" type="datetime"/>
<describe label="工单描述" length="1" must="1" type="textedit"/>
<code label="编号" readonly="y" must="2" type="textedit"/>
<lastcode label="上个工单编号" type="textview"/>
<targetplace label="目的地" length="1" must="2" type="textedit"/>
<date label="发起日期" readonly="y1" must="2" type="datetime"/>
<planend label="计划截止时间" length="1" must="2" type="datetime"/>
<sender label="发起人" readonly="y" must="2" type="textedit"/>
<job label="发起岗位" length="0" defaults="测试岗位1,测试岗位2" must="2" type="selector"/>
</start>
<end box_title="" box_modify="" box_show="">
<score min="0" defaults="100" label="工单评分" must="2" type="number" max="100"/>
<comment label="评价内容" length="1" type="textedit"/>
</end>
<fj box_title="" box_modify="" box_show="">
<zp3 label="照片或视频" amount="3" type="media"/>
<zp1 label="照片" length="1" amount="2" type="media"/>
<dtgj label="轨迹" length="2" type="mapview"/>
<jwd2 label="当前位置" type="textedit" location="2"/>
<dtwz label="定位" length="1" type="mapview"/>
<sfwc label="是否完成" type="bool"/>
<zp2 label="视频" length="2" amount="2" type="media"/>
<jwd1 label="经纬度" type="textedit" location="1"/>
<jtnr label="具体内容" length="1" type="button"/>
</fj>
</sheet>