Beyond平君

导航

OC-Xml文件解析

本次解析使用的由Google提供的一个开源插件:GDataXmlNode对xml文件的解析。

导入GDataXmlNode文件和配置Xcode :http://blog.csdn.net/hmt20130412/article/details/24042499

 

题目:解析如下Xml文件,通过输入城市中文名,获取对应城市的首字母。 例如:输入常德  输出c

xml文件:

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3 <plist version="1.0">
  4 <dict>
  5     <key>a</key>
  6     <array>
  7         <string>安吉</string>
  8         <string>安庆</string>
  9         <string>鞍山</string>
 10         <string>澳门</string>
 11     </array>
 12     <key>b</key>
 13     <array>
 14         <string>保定</string>
 15         <string>宝鸡</string>
 16         <string>包头</string>
 17         <string>北海</string>
 18         <string>北京</string>
 19         <string>蚌埠</string>
 20         <string>滨州</string>
 21     </array>
 22     <key>c</key>
 23     <array>
 24         <string>沧州</string>
 25         <string>长春</string>
 26         <string>常德</string>
 27         <string>长乐</string>
 28         <string>长沙</string>
 29         <string>常熟</string>
 30         <string>常州</string>
 31         <string>潮阳</string>
 32         <string>潮州</string>
 33         <string>承德</string>
 34         <string>成都</string>
 35         <string>郴州</string>
 36         <string>重庆</string>
 37         <string>滁州</string>
 38         <string>慈禧</string>
 39         <string>从化</string>
 40     </array>
 41     <key>d</key>
 42     <array>
 43         <string>大连</string>
 44         <string>丹东</string>
 45         <string>大庆</string>
 46         <string>大同</string>
 47         <string>德阳</string>
 48         <string>德州</string>
 49         <string>东莞</string>
 50         <string>东营</string>
 51     </array>
 52     <key>e</key>
 53     <array>
 54         <string>鄂尔多斯</string>
 55         <string>鄂州</string>
 56     </array>
 57     <key>f</key>
 58     <array>
 59         <string>奉化</string>
 60         <string>佛山</string>
 61         <string>抚顺</string>
 62         <string>阜阳</string>
 63         <string>福州</string>
 64     </array>
 65     <key>g</key>
 66     <array>
 67         <string>赣州</string>
 68         <string>广州</string>
 69         <string>桂林</string>
 70         <string>贵阳</string>
 71     </array>
 72     <key>h</key>
 73     <array>
 74         <string>哈尔滨</string>
 75         <string>海口</string>
 76         <string>海拉尔</string>
 77         <string>邯郸</string>
 78         <string>杭州</string>
 79         <string>合肥</string>
 80         <string>鹤岗</string>
 81         <string>衡阳</string>
 82         <string>鹤山</string>
 83         <string>河源</string>
 84         <string>花都</string>
 85         <string>淮安</string>
 86         <string>淮北</string>
 87         <string>淮南</string>
 88         <string>黄石</string>
 89         <string>呼和浩特</string>
 90         <string>惠东</string>
 91         <string>惠阳</string>
 92         <string>惠州</string>
 93         <string>葫芦岛</string>
 94         <string>湖州</string>
 95         <string>怀化</string>
 96     </array>
 97     <key>j</key>
 98     <array>
 99         <string>江都</string>
100         <string>江门</string>
101         <string>姜堰</string>
102         <string>江阴</string>
103         <string>焦作</string>
104         <string>嘉兴</string>
105         <string>揭阳</string>
106         <string>吉林</string>
107         <string>济南</string>
108         <string>荆门</string>
109         <string>荆沙</string>
110         <string>金华</string>
111         <string>济宁</string>
112         <string>晋江</string>
113         <string>晋中</string>
114         <string>锦州</string>
115         <string>九江</string>
116     </array>
117     <key>k</key>
118     <array>
119         <string>开封</string>
120         <string>开平</string>
121         <string>昆明</string>
122         <string>昆山</string>
123     </array>
124     <key>l</key>
125     <array>
126         <string>莱芜</string>
127         <string>莱西</string>
128         <string>廊坊</string>
129         <string>兰州</string>
130         <string>连云港</string>
131         <string>聊城</string>
132         <string>列东</string>
133         <string>醴陵</string>
134         <string>临汾</string>
135         <string>临沂</string>
136         <string>丽水</string>
137         <string>浏阳</string>
138         <string>柳州</string>
139         <string>龙岩</string>
140         <string>六安</string>
141         <string>陆丰</string>
142         <string>漯河</string>
143         <string>洛阳</string>
144         <string>泸州</string>
145     </array>
146     <key>m</key>
147     <array>
148         <string>茂名</string>
149         <string>梅州</string>
150         <string>绵阳</string>
151     </array>
152     <key>n</key>
153     <array>
154         <string>南昌</string>
155         <string>南充</string>
156         <string>南京</string>
157         <string>南宁</string>
158         <string>南平</string>
159         <string>南通</string>
160         <string>南阳</string>
161         <string>内江</string>
162         <string>宁波</string>
163     </array>
164     <key>p</key>
165     <array>
166         <string>盘锦</string>
167         <string>番禺</string>
168         <string>平顶山</string>
169         <string>莆田</string>
170         <string>濮阳</string>
171     </array>
172     <key>q</key>
173     <array>
174         <string>启东</string>
175         <string>青岛</string>
176         <string>清远</string>
177         <string>秦皇岛</string>
178         <string>钦州</string>
179         <string>琼海</string>
180         <string>齐齐哈尔</string>
181         <string>全国</string>
182         <string>泉州</string>
183         <string>衢州</string>
184     </array>
185     <key>r</key>
186     <array>
187         <string>日照</string>
188     </array>
189     <key>s</key>
190     <array>
191         <string>三河</string>
192         <string>三亚</string>
193         <string>上海</string>
194         <string>上虞</string>
195         <string>汕头</string>
196         <string>汕尾</string>
197         <string>韶关</string>
198         <string>绍兴</string>
199         <string>邵阳</string>
200         <string>沈阳</string>
201         <string>深圳</string>
202         <string>石家庄</string>
203         <string>石狮</string>
204         <string>十堰</string>
205         <string>寿光</string>
206         <string>四会</string>
207         <string>随州</string>
208         <string>苏州</string>
209     </array>
210     <key>t</key>
211     <array>
212         <string>泰安</string>
213         <string>台山</string>
214         <string>太原</string>
215         <string>泰州</string>
216         <string>台州</string>
217         <string>塘沽</string>
218         <string>唐山</string>
219         <string>天津</string>
220         <string>铜陵</string>
221     </array>
222     <key>w</key>
223     <array>
224         <string>潍坊</string>
225         <string>威海</string>
226         <string>渭南</string>
227         <string>温州</string>
228         <string>武汉</string>
229         <string>芜湖</string>
230         <string>吴江</string>
231         <string>乌鲁木齐</string>
232         <string>无锡</string>
233         <string>梧州</string>
234     </array>
235     <key>x</key>
236     <array>
237         <string>厦门</string>
238         <string>西安</string>
239         <string>襄樊</string>
240         <string>香港</string>
241         <string>湘潭</string>
242         <string>咸宁</string>
243         <string>咸阳</string>
244         <string>兴宁</string>
245         <string>邢台</string>
246         <string>西宁</string>
247         <string>新乡</string>
248         <string>宣城</string>
249         <string>许昌</string>
250         <string>徐州</string>
251     </array>
252     <key>y</key>
253     <array>
254         <string>盐城</string>
255         <string>阳江</string>
256         <string>阳泉</string>
257         <string>阳朔</string>
258         <string>扬州</string>
259         <string>烟台</string>
260         <string>宜昌</string>
261         <string>银川</string>
262         <string>英德</string>
263         <string>义务</string>
264         <string>宜兴</string>
265         <string>仪征</string>
266         <string>永康</string>
267         <string>岳阳</string>
268         <string>云浮</string>
269         <string>玉林</string>
270         <string>玉溪</string>
271         <string>余姚</string>
272     </array>
273     <key>z</key>
274     <array>
275         <string>湛江</string>
276         <string>肇庆</string>
277         <string>招远</string>
278         <string>郑州</string>
279         <string>镇江</string>
280         <string>中山</string>
281         <string>周口</string>
282         <string>舟山</string>
283         <string>珠海</string>
284         <string>诸暨</string>
285         <string>株洲</string>
286         <string>淄博</string>
287         <string>自贡</string>
288     </array>
289 </dict>
290 </plist>
View Code

新建了一个类Xml解析类:ParseAlphAndCityName.h

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface ParseAlphAndCityName : NSObject
 4 {
 5     NSMutableDictionary *_alphAndCityNameList;
 6 }
 7 
 8 @property (retain,nonatomic)NSMutableDictionary *alphAndCityNameList;
 9 
10 
11 - (id)init;
12 
13 
14 - (void)parseXml:(NSString *)xmlFilePath;
15 
16 
17 + (void)test:(NSString *)xmlFilePath;
18 
19 @end

ParseAlphAndCityName.m

最主要的是parseXml:(NSString *)xmlFilePath;方法。利用了主流的对xml的解析方法。

不多说:看代码,已经注释的很清楚了。

 1 #import "ParseAlphAndCityName.h"
 2 #import "GDataXMLNode.h"
 3 
 4 
 5 @implementation ParseAlphAndCityName
 6 
 7 -(id)init
 8 {
 9     if (self = [super init]) {
10         self->_alphAndCityNameList = [[NSMutableDictionary alloc] init];
11     }
12     
13     return self;
14 }
15 
16 
17 - (void)parseXml:(NSString *)xmlFilePath
18 {
19     //将xml文件读取称二进制对象
20     NSData *xmlData = [[NSData alloc] initWithContentsOfFile:xmlFilePath];
21     //获取document对象,xml文档对象
22     GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
23     //获取根节点对象
24     GDataXMLElement *rootElement = [document rootElement];
25     
26     //获取指定路径的所有节点 返回值为数组对象
27     GDataXMLElement *dict = [[rootElement nodesForXPath:@"./dict" error:nil] objectAtIndex:0];
28     //获取当前节点dict下的所有名为key节点的值
29     NSArray *keyArray = [dict elementsForName:@"key"];
30     
31     //获取指定路径所有的array节点 返回值为数组对象
32     NSArray *arrayArray = [rootElement nodesForXPath:@"./dict/array" error:nil];
33     
34     NSInteger len = [keyArray count];
35     
36     //遍历
37     for (NSInteger i=0; i<len; i++) {
38         //将节点的值转为字符串对象
39         NSString *alString = [[keyArray objectAtIndex:i] stringValue];
40         
41         //创建一个可变数组存储城市名称
42         NSMutableArray *cityChinaNames = [[NSMutableArray alloc] init];
43         //获取当前array节点所有名为string节点的值
44         NSArray *elementsValue = [[arrayArray objectAtIndex:i] elementsForName:@"string"];
45         for (GDataXMLElement *item in elementsValue) {
46             //可变数组中加入城市名称
47             [cityChinaNames addObject:[item stringValue]];
48         }
49         
50         //将对应的字母和和城市数组对象添加到字典中
51         [self.alphAndCityNameList setObject:cityChinaNames forKey:alString];
52     }
53 }
54 
55 
56 + (void)test:(NSString *)xmlFilePath
57 {
58     char cityName[100] = {};
59     scanf("%s",cityName);
60     
61     ParseAlphAndCityName *parseXml = [[ParseAlphAndCityName alloc] init];
62     [parseXml parseXml:xmlFilePath];
63     
64     for (NSString *key in parseXml.alphAndCityNameList) {
65         NSArray *citys = [parseXml.alphAndCityNameList objectForKey:key];
66         if ([citys containsObject:[NSString stringWithUTF8String:cityName]]) {
67             NSLog(@"%@",key);
68         }
69     }
70     
71 }
72 
73 @end

main.m文件

#import "ParseAlphAndCityName.h"

#define XmlFilePath @"/Users/qianfeng/Desktop/day23_OC13_xml文件/day23_OC13_XML城市解析_练习/qfile.xml"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        [ParseAlphAndCityName test:XmlFilePath];
        
    }
    return 0;
}

 

posted on 2015-06-26 11:30  Beyond平君  阅读(598)  评论(0编辑  收藏  举报