最近工作做一个项目,需要用到对xml数据的解析,还有个奇葩需求,就是要修改节点属性,根据节点name的属性是否包含http/https,如果有,在下载对应的模型数据,并且修改节点模型的绝对路径;楼主拿到这个需求,也是很蒙,现在都是使用JSON数据了,我们却要用XML写配置文件!!!呜呜呜,好话不多说了,开始搞吧,老板说的都是对的;
然后楼主开始查资料,我找啊找,找啊找,最终找到两款三方库,GDataXML和KissXML,废话不多少,楼主直接上干货,
第一种方案,使用GDataXML解析,最终反正遇到各种,关键是不能单个修改,可能是小弟学艺不精,纠结了半个小时后,这种方法果断放弃了;
第二种方案:使用KissXML对xml配置文件进行解析,获取节点属性,并且进行修改,重新生成XML文件
第一步,废话不多说,先上干货,示例demo,https://github.com/EdwinXiang/ParasingXML,然后待我来分析一下
1、关于xml的基础知识我就不多说了,不会的去网上看对应资料;拿到KissXML后,显示查看了他的提供的方法和属性接口,大概明白了什么意思,然后再根据实例demo开始写我的工程了,导入KissXML库,这里需要加入依赖库,libxml2.tbd,然后需要对xml进行配置,不然会报错找不到文件
2、环境配置好了,咱们开始码代码吧
1、导入头文件
#import "DDXML.h"
#import "DDXMLElementAdditions.h"
2、读取xml文件内容
-(void)readXmlContent {
NSString *sanboxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString *path = [[NSString alloc]initWithFormat:@"%@/示例频道包",sanboxPath];
NSString *channelConfigxmlPath = [path stringByAppendingPathComponent:@"channelConfig.xml"];
NSData *data = [[NSData alloc]initWithContentsOfFile:channelConfigxmlPath];
//修改数据模型路径
[self parasingXmlData:data withKeyXML:@"//TrackingData//media"];
}
//解析xml文件内容
-(void)parasingXmlData:(NSData *)data withKeyXML:(NSString *)kXML{
DDXMLDocument *doc = [[DDXMLDocument alloc]initWithData:data options:0 error:nil];
//解析
//修改media文件路径
NSArray *items = [doc nodesForXPath:kXML error:nil];
NSLog(@"items = %@",items);
for (int i = 0; i<items.count; i++) {
DDXMLElement *element = items[i];
DDXMLNode *auser = [element attributeForName:@"name"];
NSString *str = auser.stringValue;
NSLog(@"str == %@",str);
if ([str hasPrefix:@"http"] || [str hasPrefix:@"https"]) {
NSArray *arr = [str componentsSeparatedByString:@"/"];
NSString *fileName = [arr lastObject];
NSString *mediaName = [self downLoadWithUrl:str withFileName:fileName];
NSLog(@"media = %@",mediaName);
//分割数组
NSArray *mediaArr = [mediaName componentsSeparatedByString:@"/"];
NSLog(@"数组:%@",mediaArr);
//拼接字符串
NSString *resultStr = [NSString stringWithFormat:@"assets/media/%@",mediaArr.lastObject];
[auser setStringValue:[NSString stringWithFormat:@"%@",resultStr]];
}
}
//修改识别图路径
//解析
NSArray *Iconitems = [doc nodesForXPath:@"//TrackingData//dataset" error:nil];
NSLog(@"itemscount = %ld",(unsigned long)Iconitems.count);
for (int i = 0; i<Iconitems.count; i++) {
DDXMLElement *element = Iconitems[i];
DDXMLNode *auser = [element attributeForName:@"name"];
NSString *str = auser.stringValue;
NSLog(@"str == %@",auser);
if ([str hasPrefix:@"http"] || [str hasPrefix:@"https"]) {
NSArray *arr = [str componentsSeparatedByString:@"/"];
NSString *fileName = [arr lastObject];
NSString *datasetName = [self downLoadWithUrl:str withFileName:fileName];
NSLog(@"media = %@",datasetName);
//分割数组
NSArray *datasetArr = [datasetName componentsSeparatedByString:@"/"];
NSLog(@"数组:%@",datasetArr);
//拼接字符串
NSString *resultStr = [NSString stringWithFormat:@"assets/dataset/%@",datasetArr.lastObject];
[auser setStringValue:[NSString stringWithFormat:@"%@",resultStr]];
}
}
//保存到沙盒目录下
NSString *path =[[NSString alloc]initWithFormat:@"%@/changeXmlData.xml", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0]];
NSString *result=[[NSString alloc]initWithFormat:@"%@",doc];
//写入数据
[result writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
3、最终达到了我们想要的结果,可在沙盒查看保存的xml配置文件
实例demo请在github上面下载,如果本文章对你有用,请给个star吧,也算是给我的鼓励,谢谢!