<?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>
//
// main.m
// OC2-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/内存管理/OC2-xml文件解析/Student.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(@"class =%@",school);
NSArray *students =[rootElement elementsForName:
@"student"];
for (GDataXMLElement *student
in students) {
NSString *sign =[[student attributeForName:
@"sign"] stringValue];
if(sign)
{
NSLog(@"----%@-----",sign);
}
NSString *name =[[[student elementsForName:
@"name"] firstObject] stringValue];
NSLog(@" name =%@ ",name);
NSString *sex =[[[student elementsForName:
@"sex"] firstObject] stringValue];
NSLog(@" sex =%@ ",sex);
NSString *phone =[[[student elementsForName:
@"phone"] firstObject] stringValue];
NSLog(@" phone =%@ ",phone);
}
}
return 0;
}
转载于:https://www.cnblogs.com/0515offer/p/4599587.html