20150625_OC之XML格式解析

前端之家收集整理的这篇文章主要介绍了20150625_OC之XML格式解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
一下对XML格式的解析使用的是谷歌的第三方库XdataxML,在进行XML解析的是时候需要对Xcode进行配置,将第三方库导入使用

例1:
//
//  main.m
//  IOS150625_ObjectiveC_XML文件解析
//
//  Created by PengJunlong on 15/6/25.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GdataxMLNode.h"
//************************
//*      XML文档解析      *
//************************

//使用第三方库
//XML
//由 <>开始,</>结束
//1.添加libxml2.dylib库,在build Phases中添加二进制库libxml2.dylib,//2.在build Setting中Search Path-->Header Search Path-->添加文件路径/usr/include/libxml2
//3.添加GdataxMLNode库,将GdataxML文件中的GdataxMLNode.h和GdataxMLNode.m文件添加进工程中,在Build Phases-->Compile Source中修改GdataxMLNode.m文件为arc与mrc混合编程 -fno-objc-arc
int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSData *xmlData = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150625_ObjectiveC_XML文件解析1/IOS150625_ObjectiveC_XML文件解析/users.xml"];
        //创建document对象
        GdataxMLDocument *document = [[GdataxMLDocument alloc] initWithData:xmlData options:0 error:nil];
        //获取根节点
        GdataxMLElement *rootElement = [document rootElement];
        NSArray *users = [rootElement elementsForName:@"Users"];
        NSLog(@"%@",users);
        for (GdataxMLElement *user in users) {
            NSString *ID = [[user attributeForName:@"id"] stringValue];
            NSLog(@"id = %@",ID);
            NSString *name = [[[user elementsForName:@"name"] firstObject] stringValue];
            NSLog(@"name = %@",name);
            NSString *age = [[[user elementsForName:@"age"] firstObject] stringValue];
            NSLog(@"age = %@",age);
        }
    }
    return 0;
}
users.xml如下:
<?xml version = "1.0" encoding="utf-8"?>
<Users>
    <User id="001">
        <name>Ryan</name>
        <age>24</age>
    </User>
    <User id ="002">
        <name>Tang</name>
        <age>23</age>
    </User>
</Users>

例2:

//
//  main.m
//  IOS150625_ObjectiveC_XML解析2
//
//  Created by PengJunlong on 15/6/25.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GdataxMLNode.h"
int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSData *xmlData = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150625_ObjectiveC_XML解析2/Student.xml"];
        //创建XML document
        GdataxMLDocument *document = [[GdataxMLDocument alloc] initWithData:xmlData options:0 error:nil];
        //获取根节点
        GdataxMLElement *rootElement = [document rootElement];
        NSString *class = [[rootElement attributeForName:@"class"] stringValue];
        NSLog(@"class = %@",class);
        NSString *school = [[rootElement attributeForName:@"school"] stringValue];
        NSLog(@"school = %@",school);
        
        NSArray *studentArray = [rootElement elementsForName:@"student"];
        for (GdataxMLElement *student in studentArray)
        {
            NSString *sign = [[student attributeForName:@"sign"] stringValue];
            if (sign)
            {
                NSLog(@"------%@------",sign);
            }
            else
            {
                NSLog(@"--------------");
            }
            NSString *name = [[[student elementsForName:@"name"] firstObject] stringValue];
            NSLog(@"name = %@",name);
            
            NSString *num = [[[student elementsForName:@"number"] firstObject] stringValue];
            NSLog(@"number = %@",num);
            
            NSString *sex = [[[student elementsForName:@"sex"] firstObject] stringValue];
            NSLog(@"sex = %@",sex);
            
            NSString *phone = [[[student elementsForName:@"phone"] firstObject] stringValue];
            NSLog(@"phone = %@",phone);
        }
    }
    return 0;
}

Student.xml
文件如下:
<?xml version="1.0" encoding="UTF-8"?>
    <Students class="17班" school="中国">
    <student>
        <number>1</number>
        <name>胡明涛</name>
        <sex>男</sex>
        <phone>123558</phone>
    </student>
    <student>
        <number>2</number>
        <name>成风采</name>
        <sex>男</sex>
        <phone>122323</phone>
    </student>
    <student>
        <number>3</number>
        <name>陈咬金</name>
        <sex>男</sex>
        <phone>21313558</phone>
    </student>
    <student sign="非应届生">
        <number>4</number>
        <name>天天下</name>
        <sex>男</sex>
        <phone>56453558</phone>
    </student>
</Students>

例3:
//
//  main.m
//  IOS150625_ObjectiveC_XML文件解析3
//
//  Created by PengJunlong on 15/6/25.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GdataxMLNode.h"
int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSData *xmlData = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150625_ObjectiveC_XML文件解析3/citys.xml"];
        GdataxMLDocument *document = [[GdataxMLDocument alloc]initWithData:xmlData options:0 error:nil];
        GdataxMLElement *rootElement = [document rootElement];
        //XPath
        //相对路径:从当前节点计算的路径
        //绝对路径:从根节点计算的路径
        //从当前路径下获取所有节点(相对路径)
        NSArray *cityNames = [rootElement nodesForXPath:@"./cities/city/name" error:nil];
        for (GdataxMLElement *name in cityNames) {
            NSString *nameString = [[name attributeForName:@"data"] stringValue];
            NSLog(@"cityName = %@",nameString);
        }
        NSArray *city = [rootElement nodesForXPath:@"./cities/city" error:nil];
        for (GdataxMLElement *name in city) {
            NSString *defaultStr = [[name attributeForName:@"default"] stringValue];
            if (defaultStr) {
               NSLog(@"-------default = %@---------",defaultStr);
            }
            NSString *nameString = [[[[name elementsForName:@"name"] firstObject] attributeForName:@"data"] stringValue];
            NSLog(@"nameString = %@",nameString);
            NSString *latitude_e6 = [[[name elementsForName:@"latitude_e6"] firstObject] stringValue];
            NSLog(@"latitude_e6 = %@",latitude_e6);
            NSString *longitude_e6 = [[[name elementsForName:@"longitude_e6"] firstObject] stringValue];
            NSLog(@"longitude_e6 = %@",longitude_e6);
        }
//        NSLog(@"cityName = %@",cityNames);
    }
    return 0;
}

citys.xml文件如下:
<pre name="code" class="html"><?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>
 
 
例4:
//
//  main.m
//  IOS150625_ObjectiveC_XML文件解析4
//
//  Created by PengJunlong on 15/6/25.
//  Copyright (c) 2015年 Peng Junlong. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GdataxMLNode.h"
int main(int argc,const char * argv[]) {
    @autoreleasepool {
        NSData *xmlData = [NSData dataWithContentsOfFile:@"/Users/qianfeng/Public/ExerciseProject/IOS150625_ObjectiveC_XML文件解析4/qfile.xml"];
        GdataxMLDocument *document = [[GdataxMLDocument alloc] initWithData:xmlData options:0 error:nil];
        GdataxMLElement *rootElement = [document rootElement];
        GdataxMLElement *keyToCity = [[rootElement nodesForXPath:@"./dict" error:nil] firstObject];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:20];
        NSArray *keys = [keyToCity elementsForName:@"key"];
        NSArray *citys =[keyToCity elementsForName:@"array"];
        for (int i=0; i<[keys count]; i++) {
            NSArray *cities = [[citys objectAtIndex:i] elementsForName:@"string"];
            [dict setObject:cities forKey:[[keys objectAtIndex:i] stringValue]];
        }
        char ch[20]={};
        printf("输入城市名:");
        scanf("%s",ch);
        NSString *cityNameCh = [NSString stringWithUTF8String:ch];
        for (NSString *key in dict)
        {
            NSArray *cityName = [dict objectForKey:key];
            for (GdataxMLElement *city in cityName)
            {
                if ([[city stringValue]isEqualToString:cityNameCh])
                {
                    NSLog(@"Key = %@",key);
                }
            }
        }
        
    }
    return 0;
}

文件格式如下:
 
 
<pre name="code" class="html"><?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>
</dict>
</plist>
原文链接:https://www.f2er.com/xml/296540.html

猜你在找的XML相关文章