码迷,mamicode.com
首页 > 移动开发 > 详细

IOS数据解析之XML

时间:2015-10-23 23:00:19      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:

XML

1.定义

  • 指可扩展标记语言()
  • XML HTML
  • XML 传输数据,而非显示数据
  • XML XML 被设计为具有自我描述性。
<?xml version="1.0" encoding="utf-8"?>
<Users>
    <User>
        <name id = "201502004">张三</name>
        <age>24</age>
    </User>
    <User>
        <name id = "201502002">李四</name>
        <age>23</age>
    </User>
</Users>

2.解析

1) KissXML

*需导入KissXML库

*进入Build Settings,在搜索框中搜索Head Search Path,然后双击并点击+按钮添加/usr/include/libxml2

*然后搜索Other linker flags,同样的方式添加-lxml2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 1.获取xml地址
    NSString *path = [[NSBundle mainBundle]pathForResource:@"user" ofType:@"xml"];
    // 2.获得本地urlString
    NSString *content = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:path] encoding:NSUTF8StringEncoding error:nil];
    // 3.解析
    DDXMLDocument *document = [[DDXMLDocument alloc]initWithXMLString:content options:0 error:nil];
    // 4.通过xpath找到指定节点
    NSArray *elements = [document nodesForXPath:@"/Users/User" error:nil];
    // 5.遍历
    for (DDXMLElement *element in elements) {
        // 取到name
        DDXMLElement *name = [element elementsForName:@"name"][0];
        // 得到name的属性
        NSArray *node = [document nodesForXPath:@"/Users/User/name" error:nil];
        DDXMLNode *nodeID = [node[0] attributeForName:@"id"];
        // 得到age
        DDXMLElement *age = [element elementsForName:@"age"][0];
        NSLog(@"name = %@, age = %@, id = %@",[name stringValue],[age stringValue],[nodeID stringValue]);
    }
}

2)GData(MRC下)

*导入GDataXMLNode库

* 找到Build OPhases - > Compile Sources ->GDataXMLNode.m双击添加 -fno-objc-arc

*进入Build Settings,在搜索框中搜索Head Search Path,然后双击并点击+按钮添加/usr/include/libxml2

*然后搜索Other linker flags,同样的方式添加-lxml2

- (void)GData{
    // 1.获得path
    NSString *path = [[NSBundle mainBundle]pathForResource:@"user" ofType:@"xml"];
    // 2.得到content
    NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 3.得到节点
    GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:content options:0 error:nil];
    NSArray *elements = [document nodesForXPath:@"/Users/User" error:nil];
    for (GDataXMLElement *element in elements) {
        // 取到name
        GDataXMLElement *name = [element elementsForName:@"name"][0];
        // 得到name的属性
        NSArray *node = [document nodesForXPath:@"/Users/User/name" error:nil];
        GDataXMLElement *nodeID = [node[0] attributeForName:@"id"];
        // 得到age
        GDataXMLElement *age = [element elementsForName:@"age"][0];
        NSLog(@"name = %@, age = %@, id = %@",[name stringValue],[age stringValue],[nodeID stringValue]);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

3.定义XML

*使用KissXML

- (void)createXML
{
    // 1.创建根元素
    DDXMLElement *rootElement = [[DDXMLElement alloc] initWithName:@"Users"];
    for (int i = 0; i < 2; i++)
    {
        // 2.User子元素
        DDXMLElement *userElement = [[DDXMLElement alloc] initWithName:@"User"];
        // name元素
        DDXMLElement *nameElement = [[DDXMLElement alloc] initWithName:@"name" stringValue:[NSString stringWithFormat:@"张三%d",i+1]];
        // age元素
        DDXMLElement *ageElement = [[DDXMLElement alloc] initWithName:@"age" stringValue:[NSString stringWithFormat:@"%d",20+i]];
        // 5.添加子元素
        [userElement addChild:nameElement];
        [userElement addChild:ageElement];
        // 6.在nameElement中添加属性
        DDXMLNode *node = [DDXMLNode attributeWithName:@"id" stringValue:@"xxxx"];
        [nameElement addAttribute:node];
        // 7.把User添加到根元素中
        [rootElement addChild:userElement];
    }
    NSLog(@"--%@",[rootElement description]);
}

 

IOS数据解析之XML

标签:

原文地址:http://www.cnblogs.com/3WWanXiang/p/4905857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!