首页 技术 正文
技术 2022年11月13日
0 收藏 719 点赞 4,778 浏览 7545 个字

iOS对于XML的解析有系统自带的SDK–NSXMLParser,鄙人愚拙,只会用GDataXML进行解析,这里仅介绍GData的使用。(以下图为例)

【IOS】 XML解析和xml转plist文件(GDataXML)

1.对于一个xml文件,先读取出来

NSData *xmlData = [[NSFileManager defaultManager]contentsAtPath:[NSString stringWithFormat:@”%@/%@”,[[NSBundle mainBundle] resourcePath],@”test.xml”]];

NSError *error;

GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];

2.取到要解析到的节点目录

NSString *path = @”/Users/user”;

NSArray *arr = [document nodesForXPath:path error:nil];

//获取元素GDataXMLElementGDataXMLElement *subElement = [arr lastObject]; //获取此节点下的name(如果此节点下是单一string可以这样)NSString *name = [[[subElement elementsForName:@”name”]lastObject] stringValue]; //如果节点下是几个元素的,例如<name name1 = “aa” name2 = “aaa”/> 则应该

NSArray *userArr = [subElement elementsForName:@”name”];

for (GDataXMLElement *subElement in userArr) {

NSString *name1 = [[subElement attributeForName:@”name1″]stringValue];

NSString *name2 = [[subElement attributeForName:@”name2″]stringValue];

}

至此可解析完毕。

3.下面实例展示如果将一份全球国家地区和编码的xml文件转为plist文件

【IOS】 XML解析和xml转plist文件(GDataXML)

NSData *plistXML = [[NSFileManager defaultManager]contentsAtPath:[NSString stringWithFormat:@”%@/%@”,[[NSBundle mainBundle] resourcePath],@”country_city_20151105.xml”]];

NSError *error;

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:plistXML options:0 error:&error];

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

NSString *path = @”/Location”;

NSArray *locationArr = [doc nodesForXPath:path error:nil];

NSLog(@”locationArr = %@”,locationArr);

for (GDataXMLElement *obj in locationArr) {

NSMutableArray *countryA = [[NSMutableArray alloc]init];

NSMutableArray *countryB = [[NSMutableArray alloc]init];

NSMutableArray *countryC = [[NSMutableArray alloc]init];

NSMutableArray *countryD = [[NSMutableArray alloc]init];

NSMutableArray *countryE = [[NSMutableArray alloc]init];

NSMutableArray *countryF = [[NSMutableArray alloc]init];

NSMutableArray *countryG = [[NSMutableArray alloc]init];

NSMutableArray *countryH = [[NSMutableArray alloc]init];

NSMutableArray *countryI = [[NSMutableArray alloc]init];

NSMutableArray *countryJ = [[NSMutableArray alloc]init];

NSMutableArray *countryK = [[NSMutableArray alloc]init];

NSMutableArray *countryL = [[NSMutableArray alloc]init];

NSMutableArray *countryM = [[NSMutableArray alloc]init];

NSMutableArray *countryN = [[NSMutableArray alloc]init];

NSMutableArray *countryO = [[NSMutableArray alloc]init];

NSMutableArray *countryP = [[NSMutableArray alloc]init];

NSMutableArray *countryQ = [[NSMutableArray alloc]init];

NSMutableArray *countryR = [[NSMutableArray alloc]init];

NSMutableArray *countryS = [[NSMutableArray alloc]init];

NSMutableArray *countryT = [[NSMutableArray alloc]init];

NSMutableArray *countryU = [[NSMutableArray alloc]init];

NSMutableArray *countryV = [[NSMutableArray alloc]init];

NSMutableArray *countryW = [[NSMutableArray alloc]init];

NSMutableArray *countryX = [[NSMutableArray alloc]init];

NSMutableArray *countryY = [[NSMutableArray alloc]init];

NSMutableArray *countryZ = [[NSMutableArray alloc]init];

NSArray *countryArr = [obj elementsForName:@”CountryRegion”];

if (countryArr.count) {

for (GDataXMLElement *subElement in countryArr) {

NSString *NameString = [[subElement attributeForName:@”Name”]stringValue];

NSString *codeStr = [[subElement attributeForName:@”Code”]stringValue];

NSString *regionCodeStr = [[subElement attributeForName:@”regionCode”]stringValue];

NSLog(@”NameString = %@ codeStr = %@ regionCodeStr = %@”,NameString,codeStr,regionCodeStr);

NSString *tmpStr = [NSString stringWithFormat:@”%@+%@”,NameString,regionCodeStr];

if ([@”A” isEqualToString:[self firstLetterForString:NameString]]) {

[countryA addObject:tmpStr];

}else if ([@”B” isEqualToString:[self firstLetterForString:NameString]]) {

[countryB addObject:tmpStr];

}else if ([@”C” isEqualToString:[self firstLetterForString:NameString]]) {

[countryC addObject:tmpStr];

}else if ([@”D” isEqualToString:[self firstLetterForString:NameString]]) {

[countryD addObject:tmpStr];

}else if ([@”E” isEqualToString:[self firstLetterForString:NameString]]) {

[countryE addObject:tmpStr];

}else if ([@”F” isEqualToString:[self firstLetterForString:NameString]]) {

[countryF addObject:tmpStr];

}else if ([@”G” isEqualToString:[self firstLetterForString:NameString]]) {

[countryG addObject:tmpStr];

}else if ([@”H” isEqualToString:[self firstLetterForString:NameString]]) {

[countryH addObject:tmpStr];

}else if ([@”I” isEqualToString:[self firstLetterForString:NameString]]) {

[countryI addObject:tmpStr];

}else if ([@”J” isEqualToString:[self firstLetterForString:NameString]]) {

[countryJ addObject:tmpStr];

}else if ([@”K” isEqualToString:[self firstLetterForString:NameString]]) {

[countryK addObject:tmpStr];

}else if ([@”L” isEqualToString:[self firstLetterForString:NameString]]) {

[countryL addObject:tmpStr];

}else if ([@”M” isEqualToString:[self firstLetterForString:NameString]]) {

[countryM addObject:tmpStr];

}else if ([@”N” isEqualToString:[self firstLetterForString:NameString]]) {

[countryN addObject:tmpStr];

}else if ([@”O” isEqualToString:[self firstLetterForString:NameString]]) {

[countryO addObject:tmpStr];

}else if ([@”P” isEqualToString:[self firstLetterForString:NameString]]) {

[countryP addObject:tmpStr];

}else if ([@”Q” isEqualToString:[self firstLetterForString:NameString]]) {

[countryQ addObject:tmpStr];

}else if ([@”R” isEqualToString:[self firstLetterForString:NameString]]) {

[countryR addObject:tmpStr];

}else if ([@”S” isEqualToString:[self firstLetterForString:NameString]]) {

[countryS addObject:tmpStr];

}else if ([@”T” isEqualToString:[self firstLetterForString:NameString]]) {

[countryT addObject:tmpStr];

}else if ([@”U” isEqualToString:[self firstLetterForString:NameString]]) {

[countryU addObject:tmpStr];

}else if ([@”V” isEqualToString:[self firstLetterForString:NameString]]) {

[countryV addObject:tmpStr];

}else if ([@”W” isEqualToString:[self firstLetterForString:NameString]]) {

[countryW addObject:tmpStr];

}else if ([@”X” isEqualToString:[self firstLetterForString:NameString]]) {

[countryX addObject:tmpStr];

}else if ([@”Y” isEqualToString:[self firstLetterForString:NameString]]) {

[countryY addObject:tmpStr];

}else if ([@”Z” isEqualToString:[self firstLetterForString:NameString]]) {

[countryZ addObject:tmpStr];

}

}

}

[dict setObject:countryA forKey:@”A”];

[dict setObject:countryB forKey:@”B”];

[dict setObject:countryC forKey:@”C”];

[dict setObject:countryD forKey:@”D”];

[dict setObject:countryE forKey:@”E”];

[dict setObject:countryF forKey:@”F”];

[dict setObject:countryG forKey:@”G”];

[dict setObject:countryH forKey:@”H”];

[dict setObject:countryI forKey:@”I”];

[dict setObject:countryJ forKey:@”J”];

[dict setObject:countryK forKey:@”K”];

[dict setObject:countryL forKey:@”L”];

[dict setObject:countryM forKey:@”M”];

[dict setObject:countryN forKey:@”N”];

[dict setObject:countryO forKey:@”O”];

[dict setObject:countryP forKey:@”P”];

[dict setObject:countryQ forKey:@”Q”];

[dict setObject:countryR forKey:@”R”];

[dict setObject:countryS forKey:@”S”];

[dict setObject:countryT forKey:@”T”];

[dict setObject:countryU forKey:@”U”];

[dict setObject:countryV forKey:@”V”];

[dict setObject:countryW forKey:@”W”];

[dict setObject:countryX forKey:@”X”];

[dict setObject:countryY forKey:@”Y”];

[dict setObject:countryZ forKey:@”Z”];

NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *plistPath = [[arr objectAtIndex:0] stringByAppendingPathComponent:@”country.plist”];

[dict writeToFile:plistPath atomically:YES];

}

#pragma mark – 取出名字首字母

-(NSString *)firstLetterForString:(NSString *)str{

ChineseString *chineseString = [[ChineseString alloc]init];

chineseString.string = [NSString stringWithString:str];

if (![chineseString.string isEqualToString:@””]) {

NSString *pinYinResult = [NSString string];

for (int i = 0; i < chineseString.string.length; i++) {

NSString *singlePinyinLetter = [[NSString stringWithFormat:@”%c”,pinyinFirstLetter([chineseString.string characterAtIndex:i])]uppercaseString];

pinYinResult = [pinYinResult stringByAppendingString:singlePinyinLetter];

}

chineseString.pinyin = [pinYinResult substringToIndex:1];

}else{

chineseString.pinyin = @”#”;

}

return chineseString.pinyin;

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,496
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,743
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,496
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,134
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298