使用NSParserXML和GDataXML 解析xml
前端之家收集整理的这篇文章主要介绍了
使用NSParserXML和GDataXML 解析xml,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用NSParserXML
01 |
<?xml version= "1.0" encoding= "UTF-8" ?> |
03 |
<message>用户信息</message> |
04
<user> |
06
<age>10</age> |
07 |
<school>JiangSu University</school> |
08
</user> |
10
<name>毒虫</name> |
12
<school>NanJing University</school> |
14
15 |
<name>女神</name> |
16
<age>23</age> |
17 |
<school>HongKong University</school> |
18
19 |
</AllUsers> |
我们用一个数组来存放,最终数据结构为
{
},
age = 10;
name ="芳仔小脚印" school = "JiangSu University" ; |
"毒虫""NanJing University"
age = 23;
"女神""HongKong University" } |
20
) |
解析步骤
一、声明代理NSXMLParserDelegate
二、解析
NSArray *keyElements = [[NSArray alloc] initWithObjects:@
"message"
,@
"user"
NSArray *rootElements = [[NSArray alloc] initWithObjects:@
"name"
"age"
"school"
// 获取xml文件的路径
NSString *xmlPath = [[NSBundle mainBundle] pathForResource:@"users"
ofType:@
"xml"
];
NSData *data = [[NSData alloc] initWithContentsOfFile:xmlPath];
// 初始化
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data]; |
xmlParser.delegate = self;
BOOL
flag = [xmlParser parse];
NSLog(@"解析成功"
);
22
} |
中间变量,在.m的interface的中定义
1
NSString *currentElement; |
2
3 |
NSString *currentValue; |
4
5 |
NSMutableDictionary *rootDic; |
6
7 |
NSMutableArray *finalArray; |
代理方法
#pragma - mark 开始解析时
-(void
)parserDidStartDocument:(NSXMLParser *)parser
finalArray = [[NSMutableArray alloc] init]; |
}
)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{ // 在这里可以打出 attributeDict |
for(NSString *key in self.keyElements){
([elementName isEqualToString:key]) { |
// 关键节点开始时,初始化一个字典来存放值
rootDic = [[NSMutableDictionary alloc] initWithCapacity:0];
(NSString *element in self.rootElements){
([element isEqualToString:element]) {
23 |
currentElement = elementName; |
24
currentValue = [NSString string]; |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
#pragma - mark 发现节点值时 |
32
|
33 |
)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string |
34
35 |
36 |
(currentElement) { |
38
currentValue = string; |
39 |
[rootDic setObject:string forKey:currentElement]; |
40
41 |
42 |
43 |
#pragma - mark 结束节点时 |
44
)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName |
45 |
46 |
47 |
[rootDic setObject:currentValue forKey:currentElement]; |
48
currentElement = nil; |
50
51 |
(NSString *key in self.keyElements){ |
52
53 |
54 |
// 关键节点结束时,将字典存放在数组中 |
56
57 |
[finalArray addObject:rootDic]; |
58
59 |
60 |
61 |
62 |
#pragma - mark 结束解析 |
63 |
)parserDidEndDocument:(NSXMLParser *)parser |
64
65 |
66 |
} |
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
- {
- self.window=[[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
-
- self.window.backgroundColor=[UIColorwhiteColor];
- [self.windowmakeKeyAndVisible];
-
- //获取工程目录的xml文件
- NSString*filePath=[[NSBundlemainBundle]pathForResource:@"users"ofType:@"xml"];
- NSData*xmlData=[[NSDataalloc]initWithContentsOfFile:filePath];
- //使用NSData对象初始化
- GdataxMLDocument*doc=[[GdataxMLDocumentalloc]initWithData:xmlDataoptions:0error:nil];
-
-
- GdataxMLElement*rootElement=[docrootElement];
- //获取根节点下的节点(User)
- NSArray*users=[rootElementelementsForName:@"User"];
- for(GdataxMLElement*userinusers){
- //User节点的id属性
- NSString*userId=[[userattributeForName:@"id"]stringValue];
- NSLog(@"Useridis:%@",userId);
- //获取name节点的值
- GdataxMLElement*nameElement=[[userelementsForName:@"name"]objectAtIndex:0];
- NSString*name=[nameElementstringValue];
- NSLog(@"Usernameis:%@",name);
- //获取age节点的值
- GdataxMLElement*ageElement=[[userelementsForName:@"age"]objectAtIndex:0];
- NSString*age=[ageElementstringValue];
- NSLog(@"Userageis:%@",age);
- NSLog(@"-------------------");
- }
- returnYES;
- }