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

时间:2021-08-10 07:39:15

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];

//获取元素GDataXMLElement
GDataXMLElement *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;

}