GDataXMLNode创建xml:
#import <Foundation/Foundation.h> 2 #import "GDataXMLNode.h" 3 4 int main() 5 { 6 NSError *error; 7 //创建根节点 8 GDataXMLElement *rootElement = [GDataXMLNode elementWithName:@"abc"]; 9 //创建第一个子节点 10 GDataXMLElement *element = [GDataXMLNode elementWithName:@"name" stringValue:@"东 11 方红"]; 12 //添加子节点到根节点上 13 [rootElement addChild: element]; 14 15 //使用根节点创建xml文档 16 GDataXMLDocument *rootDoc = [[GDataXMLDocument alloc] initWithRootElement:rootElement]; 17 //设置使用的xml版本号 18 [rootDoc setVersion:@"1.0"]; 19 //设置xml文档的字符编码 20 [rootDoc setCharacterEncoding:@"utf-8"]; 21 22 //获取并打印xml字符串 23 NSString *str = [[NSString alloc] initWithData:rootDoc.XMLData encoding:NSUTF8StringEncoding]; 24 NSLog(@"%@", str); 25 26 return 0; 27 }
编译命令为:
1 clang main.m GDataXMLNode.m -framework Foundation -lxml2 -I /usr/include/libxml2/
因为GDataXMLNode里使用了xml2,所以要记得将它连接进来,并且需要指定头文件搜索路径。还有一个需要注意的就是记得用setCharacterEncoding指定字符集。
-------------------------------------------------------------------------------------------------------------------------
city.xml
<省份>
<河北>
<city>石家庄</city>
<city>唐山</city>
<city>秦皇岛</city>
<city>邯郸</city>
<city>邢台</city>
<city>保定</city>
<city>承德</city>
<city>沧州</city>
<city>廊坊</city>
<city>衡水</city>
</河北>
<河南>
<city>郑州</city>
<city>开封</city>
<city>洛阳</city>
<city>平顶山</city>
<city>安阳</city>
<city>鹤壁</city>
<city>新乡</city>
<city>焦作</city>
<city>濮阳</city>
<city>许昌</city>
<city>漯河</city>
<city>三门峡</city>
<city>南阳</city>
<city>商丘</city>
<city>信阳</city>
<city>周口</city>
<city>驻马店</city>
</河南>
</省份>
- (void)getData
{
//一个字典
self.dic = [[NSMutableDictionary alloc] initWithCapacity:10];
// 获取包里面city.xml路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"xml"];
//二进制数据
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
//GDataXMLDocument开始解析xml
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]
//获得根节点
GDataXMLElement *root = document.rootElement;
//根节点下的所有子节点(不包括孙子节点等)
NSArray *rootson = [root children];
// 遍历根节点的子节点
for (GDataXMLElement * element in rootson) {
//所有的city按以省名为key放入字典中
NSMutableArray *ary = [self.dic objectForKey:element.name];
if (ary == nil) {
ary = [[NSMutableArray alloc] initWithCapacity:20];
[self.dic setObject:ary forKey:element.name];
}
NSArray *citys = element.children;
for (GDataXMLElement *city in citys) {
//[self.dic setObject:city.stringValue forKey:city.name];
[ary addObject:city.stringValue];
}
}
}
2.第二种遍历方法
- (void)getData1
{
self.dic = [[NSMutableDictionary alloc] initWithCapacity:10];
NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
GDataXMLElement *root = document.rootElement;
//下面解析开始 此种方法采用 nodesForXpath
NSArray *prov = @[@"河南",@"河北"];
for(int i=0 ;i<[prov count];i++)
{
//通过 nodesForXPath 可以获得root节点(此处root为根节点,也可以不是根节点)下得所有 符合//%@/city 格式的节点,
//%@是 河南,河北等, 通过此格式,获得所有的city节点(可以是孙子节点),限制是此city节点的父节点必须是 %@
NSArray *array = [root nodesForXPath:[NSString stringWithFormat:@"//%@/city",prov[i]] error:nil];
//获得所有的 河南(或河北)下的city后,以河南(河北)为key放入字典
for (GDataXMLElement *element in array) {
NSMutableArray *ary = [self.dic objectForKey:prov[i]];
if (ary == nil) {
ary = [[NSMutableArray alloc] initWithCapacity:20];
[self.dic setObject:ary forKey:prov[i]];
}
[ary addObject:element.stringValue];
}
}
}
注:
elementsForName这个方法也可以通过名字获得节点 ,和nodesForXPath类似,但仅限于获得子节点