<?xml version="1.0"?>
<xml_api_reply version="1">
<cities>
<city>
<name data="保定"/>
<latitude_e6> 38849998
</latitude_e6>
<longitude_e6> 115569999
</longitude_e6>
</city>
<city default="true" >
<name data="北京"/>
<latitude_e6> 39930000
</latitude_e6>
<longitude_e6> 116279998
</longitude_e6>
</city>
<city>
<name data="沈阳"/>
<latitude_e6> 41770000
</latitude_e6>
<longitude_e6> 123430000
</longitude_e6>
</city>
<city>
<name data="成都"/>
<latitude_e6> 30670000
</latitude_e6>
<longitude_e6> 104019996
</longitude_e6>
</city>
<city>
<name data="大连"/>
<latitude_e6> 38900001
</latitude_e6>
<longitude_e6> 121629997
</longitude_e6>
</city>
<city>
<name data="福州"/>
<latitude_e6> 26079999
</latitude_e6>
<longitude_e6> 119279998
</longitude_e6>
</city>
<city>
<name data="阜阳"/>
<latitude_e6> 32930000
</latitude_e6>
<longitude_e6> 115830001
</longitude_e6>
</city>
<city>
<name data="赣州"/>
<latitude_e6> 25850000
</latitude_e6>
<longitude_e6> 114949997
</longitude_e6>
</city>
<city>
<name data="广州"/>
<latitude_e6> 23129999
</latitude_e6>
<longitude_e6> 113319999
</longitude_e6>
</city>
<city>
<name data="贵阳"/>
<latitude_e6> 26579999
</latitude_e6>
<longitude_e6> 106720001
</longitude_e6>
</city>
<city>
<name data="哈尔滨"/>
<latitude_e6> 45750000
</latitude_e6>
<longitude_e6> 126769996
</longitude_e6>
</city>
<city>
<name data="海口"/>
<latitude_e6> 20030000
</latitude_e6>
<longitude_e6> 110349998
</longitude_e6>
</city>
<city>
<name data="邯郸"/>
<latitude_e6> 38029998
</latitude_e6>
<longitude_e6> 114419998
</longitude_e6>
</city>
<city>
<name data="杭州"/>
<latitude_e6> 30229999
</latitude_e6>
<longitude_e6> 120169998
</longitude_e6>
</city>
<city>
<name data="合肥"/>
<latitude_e6> 31870000
</latitude_e6>
<longitude_e6> 117230003
</longitude_e6>
</city>
<city>
<name data="菏泽"/>
<latitude_e6> 36119998
</latitude_e6>
<longitude_e6> 114370002
</longitude_e6>
</city>
<city>
<name data="衡阳"/>
<latitude_e6> 36119998
</latitude_e6>
<longitude_e6> 114370002
</longitude_e6>
</city>
</cities>
</xml_api_reply>
//
// main.m
// OC3-xml文件解析
//
// Created by qianfeng on 15/6/25.
// Copyright (c) 2015年 qianfeng. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GDataXMLNode.h"
int main(
int argc,
const char *
argv[]) {
@autoreleasepool {
NSData *xmlData =[NSData dataWithContentsOfFile:
@"/Users/qianfeng/Desktop/text/text/untitled folder/内存管理/OC3-xml文件解析/citys.xml"];
//创建docmentd对象
GDataXMLDocument *xmlDocument =[[GDataXMLDocument alloc] initWithData:xmlData options:
0 error:nil];
GDataXMLElement *rootElement =
[xmlDocument rootElement];
//Xpath
//相对路径:从当前节点计算的路径 用点表示当前节点 (当前路径为xml_api_reply下的节点)
//绝对路径:从根节点计算的路径 /xml_api_reply/cities/city/name (绝对路径文件最前面要加'/')
// NSArray *names = [rootElement nodesForXPath:@"/xml_api_reply/cities/city/name" error:nil];
NSArray *names = [rootElement nodesForXPath:
@"./cities/city/name" error:nil];
for (GDataXMLElement *name
in names) {
NSString *nameString =[[name attributeForName:
@"data"] stringValue];
NSLog(@"name =%@",nameString);
}
NSArray *latitudes=[rootElement nodesForXPath:
@"./cities/city" error:nil];
for (GDataXMLElement *latitude
in latitudes) {
NSString *latitudeString =[[[latitude elementsForName:
@"latitude_e6"] firstObject] stringValue];
NSLog(@"latitude_e6 =%@",latitudeString);
}
NSLog(@"name =%@",names);
}
return 0;
}
转载于:https://www.cnblogs.com/0515offer/p/4599603.html