[iOS] XML解析和xml转plist文件(GDataXML)

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

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文件

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;

}

时间: 2024-10-10 17:35:39

[iOS] XML解析和xml转plist文件(GDataXML)的相关文章

[IOS]Swift 遍历预制的本地资源文件

我事先放了一堆svg文件,但是我是批量使用的,想要直接遍历他们加入到一个list中来,那我直接就遍历他们的名称,把他们的名字组成一个array. var ss:NSString = NSBundle.mainBundle().resourcePath!//6.0.1修改了要求感叹号 println(ss) var nsfilemange = NSFileManager.defaultManager() var filelist=NSArray.alloc() filelist=nsfileman

iOS之数据解析之XML解析

XML解析常见的两种方式:DOM解析和SAX解析 DOM解析 DOM:Document Object Model(文档对象类型).解析XML时,读入整个XML文档并构建一个驻留内存的树结构(节点树),通过遍历数结构可以检索任意XML节点,读取它的属性和值,而且通常情况下,可以借助XPath,直接查询XML节点. 进行DOM方式解析数据需要使用一个第三方的类GDataXMLNode GDataXMLNode是Google提供的开源XML解析类,对libxml2.tbd进行了Objective-C的

iOS网络数据解析之XML解析-GDataXML

GDataXML 第三方xml解析框架 DOM解析:一次性将整个XML文档加载进内存,比较适合解析小文件 GDataXML中常用的类 GDataXMLDocument: 代表整个XML文档 GDataXMLElement: 代表文档中的每个元素 使用attributeForName:方法可以获得属性值 要使用GDataXML,先要对项目进行一些配置.1>导入libxml2动态库targets--Build Phases--link Binary With Libraries 2>设置libxm

XML解析与XML拼接

2015年了. 1.XML解析 这边是用的dom4j方式解析. 解析的XML如下: <?xml version="1.0" encoding="UTF-8"?> <DBSET> <R> <C N="TASKID">10001</C> <!-- TASKID --> <C N="TYPE">D</C><!-- 查询类型(D/U)

iOS中JSON解析和XML解析的区别

iOS中网络传输的格式可粗略的分为XML和JSON两种,具体的传输方式又可分为GET和POST. 其中XML格式是最早出现的,但是由于复杂,并且占用字节数相对较大,故不太适合移动端,后来简化为JSON格式. JSON本身的数据结构只有字典和数组两种,在IOS5以后出现了原生解析,而之前则有SBJson和JsonKit第三方库解析. SBJson和JsonKit的特点: 1.第三方库解析SBJson是所有解析效率里面最低的,之所以能够广泛普及,是因为其简单,封装的低耦合性,可以快速抽离出一个类,在

[转]Android中attrs.xml文件的使用详解

转自:http://blog.csdn.net/jiangwei0910410003/article/details/17006087 Android中在values中定义一个attrs.xml,然后自己定义一个组件MyView attrs.xml内容如下: [html] view plaincopy <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleabl

python xml解析之 xml.etree.ElementTree

import xml.etree.ElementTree as tree import io xml = '''<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year>2008</year> <gdppc>141100&

[iOS]开发之-字典转模型和KVC

需求一:将字典转换成自己需的对象,对象的属性是字典的key值,直接使用key值获取数据存在两个缺点,一是key值难以记忆,二是直接使用key值Xcode不会有智能提示,很容易导致未知的错误.使用模型时,可以直接通过点语法将需要的数据点出,保证数据准确无误.使用模型的优点还在于存储.传值都比较方便,不容易出错.最主要的是这也符合我们MVC和MVVM的设计模式. 注意:要使用模型就离不开KVC,假如我们从外界传入一个字典,字典中有两个 key对应的值是我们需要的,这两个值分别是 “name”.“ag

[iOS]关于 App 混合(Hybrid)开发的优化,包括H5、Weex等(本篇博客主要针对 iOS 应用讲解,但该思想同样适用于Android)

我们知道混合开发,可以节省很多成本(时间成本,经济成本等等),所以有很多公司比较钟爱这种开发形式,今天所讲的优化方式,也是我在我们公司的应用中实际用了的,而且我写的这个优化的 SDK 已经开源到 github 上了,有兴趣的同学可以下载来看一下,有不足的地方欢迎指正.这里给出链接:https://. 转载请注明出处:http://www.cnblogs.com/shisishao/p/6830736.html -.首先,先介绍下什么是混合开发: 我们通常把移动开发分为Web.Native和Hyb