OC4_XML文件解析

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>a</key>
    <array>
        <string>安吉</string>
        <string>安庆</string>
        <string>鞍山</string>
        <string>澳门</string>
    </array>
    <key>b</key>
    <array>
        <string>保定</string>
        <string>宝鸡</string>
        <string>包头</string>
        <string>北海</string>
        <string>北京</string>
        <string>蚌埠</string>
        <string>滨州</string>
    </array>
    <key>c</key>
    <array>
        <string>沧州</string>
        <string>长春</string>
        <string>常德</string>
        <string>长乐</string>
        <string>长沙</string>
        <string>常熟</string>
        <string>常州</string>
        <string>潮阳</string>
        <string>潮州</string>
        <string>承德</string>
        <string>成都</string>
        <string>郴州</string>
        <string>重庆</string>
        <string>滁州</string>
        <string>慈禧</string>
        <string>从化</string>
    </array>
    <key>d</key>
    <array>
        <string>大连</string>
        <string>丹东</string>
        <string>大庆</string>
        <string>大同</string>
        <string>德阳</string>
        <string>德州</string>
        <string>东莞</string>
        <string>东营</string>
    </array>
    <key>e</key>
    <array>
        <string>鄂尔多斯</string>
        <string>鄂州</string>
    </array>
    <key>f</key>
    <array>
        <string>奉化</string>
        <string>佛山</string>
        <string>抚顺</string>
        <string>阜阳</string>
        <string>福州</string>
    </array>
    <key>g</key>
    <array>
        <string>赣州</string>
        <string>广州</string>
        <string>桂林</string>
        <string>贵阳</string>
    </array>
    <key>h</key>
    <array>
        <string>哈尔滨</string>
        <string>海口</string>
        <string>海拉尔</string>
        <string>邯郸</string>
        <string>杭州</string>
        <string>合肥</string>
        <string>鹤岗</string>
        <string>衡阳</string>
        <string>鹤山</string>
        <string>河源</string>
        <string>花都</string>
        <string>淮安</string>
        <string>淮北</string>
        <string>淮南</string>
        <string>黄石</string>
        <string>呼和浩特</string>
        <string>惠东</string>
        <string>惠阳</string>
        <string>惠州</string>
        <string>葫芦岛</string>
        <string>湖州</string>
        <string>怀化</string>
    </array>
    <key>j</key>
    <array>
        <string>江都</string>
        <string>江门</string>
        <string>姜堰</string>
        <string>江阴</string>
        <string>焦作</string>
        <string>嘉兴</string>
        <string>揭阳</string>
        <string>吉林</string>
        <string>济南</string>
        <string>荆门</string>
        <string>荆沙</string>
        <string>金华</string>
        <string>济宁</string>
        <string>晋江</string>
        <string>晋中</string>
        <string>锦州</string>
        <string>九江</string>
    </array>
    <key>k</key>
    <array>
        <string>开封</string>
        <string>开平</string>
        <string>昆明</string>
        <string>昆山</string>
    </array>
    <key>l</key>
    <array>
        <string>莱芜</string>
        <string>莱西</string>
        <string>廊坊</string>
        <string>兰州</string>
        <string>连云港</string>
        <string>聊城</string>
        <string>列东</string>
        <string>醴陵</string>
        <string>临汾</string>
        <string>临沂</string>
        <string>丽水</string>
        <string>浏阳</string>
        <string>柳州</string>
        <string>龙岩</string>
        <string>六安</string>
        <string>陆丰</string>
        <string>漯河</string>
        <string>洛阳</string>
        <string>泸州</string>
    </array>
    <key>m</key>
    <array>
        <string>茂名</string>
        <string>梅州</string>
        <string>绵阳</string>
    </array>
    <key>n</key>
    <array>
        <string>南昌</string>
        <string>南充</string>
        <string>南京</string>
        <string>南宁</string>
        <string>南平</string>
        <string>南通</string>
        <string>南阳</string>
        <string>内江</string>
        <string>宁波</string>
    </array>
    <key>p</key>
    <array>
        <string>盘锦</string>
        <string>番禺</string>
        <string>平顶山</string>
        <string>莆田</string>
        <string>濮阳</string>
    </array>
    <key>q</key>
    <array>
        <string>启东</string>
        <string>青岛</string>
        <string>清远</string>
        <string>秦皇岛</string>
        <string>钦州</string>
        <string>琼海</string>
        <string>齐齐哈尔</string>
        <string>全国</string>
        <string>泉州</string>
        <string>衢州</string>
    </array>
    <key>r</key>
    <array>
        <string>日照</string>
    </array>
    <key>s</key>
    <array>
        <string>三河</string>
        <string>三亚</string>
        <string>上海</string>
        <string>上虞</string>
        <string>汕头</string>
        <string>汕尾</string>
        <string>韶关</string>
        <string>绍兴</string>
        <string>邵阳</string>
        <string>沈阳</string>
        <string>深圳</string>
        <string>石家庄</string>
        <string>石狮</string>
        <string>十堰</string>
        <string>寿光</string>
        <string>四会</string>
        <string>随州</string>
        <string>苏州</string>
    </array>
    <key>t</key>
    <array>
        <string>泰安</string>
        <string>台山</string>
        <string>太原</string>
        <string>泰州</string>
        <string>台州</string>
        <string>塘沽</string>
        <string>唐山</string>
        <string>天津</string>
        <string>铜陵</string>
    </array>
    <key>w</key>
    <array>
        <string>潍坊</string>
        <string>威海</string>
        <string>渭南</string>
        <string>温州</string>
        <string>武汉</string>
        <string>芜湖</string>
        <string>吴江</string>
        <string>乌鲁木齐</string>
        <string>无锡</string>
        <string>梧州</string>
    </array>
    <key>x</key>
    <array>
        <string>厦门</string>
        <string>西安</string>
        <string>襄樊</string>
        <string>香港</string>
        <string>湘潭</string>
        <string>咸宁</string>
        <string>咸阳</string>
        <string>兴宁</string>
        <string>邢台</string>
        <string>西宁</string>
        <string>新乡</string>
        <string>宣城</string>
        <string>许昌</string>
        <string>徐州</string>
    </array>
    <key>y</key>
    <array>
        <string>盐城</string>
        <string>阳江</string>
        <string>阳泉</string>
        <string>阳朔</string>
        <string>扬州</string>
        <string>烟台</string>
        <string>宜昌</string>
        <string>银川</string>
        <string>英德</string>
        <string>义务</string>
        <string>宜兴</string>
        <string>仪征</string>
        <string>永康</string>
        <string>岳阳</string>
        <string>云浮</string>
        <string>玉林</string>
        <string>玉溪</string>
        <string>余姚</string>
    </array>
    <key>z</key>
    <array>
        <string>湛江</string>
        <string>肇庆</string>
        <string>招远</string>
        <string>郑州</string>
        <string>镇江</string>
        <string>中山</string>
        <string>周口</string>
        <string>舟山</string>
        <string>珠海</string>
        <string>诸暨</string>
        <string>株洲</string>
        <string>淄博</string>
        <string>自贡</string>
    </array>
</dict>
</plist>
View Code
//
//  main.m
//  OC4_XML文件解析
//
//  Created by zhangxueming on 15/6/26.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GDataXMLNode.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSData *xmlData = [NSData dataWithContentsOfFile:@"/Users/zhangxueming/Desktop/ios1509/Day23_xml文件解析及block使用/OC4_XML文件解析/qfile.xml"];
        GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil];
        GDataXMLElement *rootElement = [document rootElement];
        GDataXMLElement *dict = [[rootElement nodesForXPath:@"./dict" error:nil] firstObject];
        NSArray *keyElements = [dict elementsForName:@"key"]; //NSArray里面存放是element对象
        
        NSArray *arrayElements = [dict elementsForName:@"array"];
        
        NSMutableDictionary *mulDict = [NSMutableDictionary dictionary];
        
        NSInteger len= [keyElements count];
        
        for (NSInteger i=0; i<len; i++) {
            NSString *key = [[keyElements objectAtIndex:i] stringValue];
            NSArray *valueElements =[[arrayElements objectAtIndex:i] elementsForName:@"string"];
            NSMutableArray *values = [NSMutableArray array];
            for (GDataXMLElement *element in valueElements) {
                [values addObject:[element stringValue]];
            }
            [mulDict setObject:values forKey:key];
        }
        
        //NSLog(@"mulDict = %@", mulDict);
        
        char buf[100]={};
        scanf("%s", buf);
        NSString *dstString = [NSString stringWithUTF8String:buf];
        
        for (NSString *key in mulDict) {
            NSArray *values = [mulDict objectForKey:key];
            if ([values containsObject:dstString]) {
                NSLog(@"key = %@", key);
            }
        }
    }
    return 0;
}

 

posted @ 2015-06-26 11:04  sirzhang  阅读(228)  评论(0编辑  收藏  举报
AmazingCounters.com