首先要加入類庫GDataXMLNode和JSON 解析本地文件Students.txt <students> <student> <name>湯姆 </name> <age>20</age> <phone>13049640144</phone> </student> <student> <name> ...
首先要加入類庫GDataXMLNode和JSON
解析本地文件Students.txt
<students>
<student>
<name>湯姆 </name>
<age>20</age>
<phone>13049640144</phone>
</student>
<student>
<name> 吉米</name>
<age>20</age>
<phone>13049640144</phone>
</student>
<student>
<name> 螺絲</name>
<age>20</age>
<phone>13049640144</phone>
</student>
</students>
//XML解析
NSString *str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Students" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
//解析XML,把結果放在document裡面
GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:str options:0 error:nil];
GDataXMLElement *root = [document rootElement];//獲得根結點
NSArray *arr = [root nodesForXPath:@"//name" error:nil];
//遍歷節點
for(GDataXMLElement *name in arr)
{
NSLog(@"%@",[name stringValue]);
}
//Json解析
NSString *str = @"[{\"name\":\"kelly\",\"age\":\"23\",\"sex\":\"女\"},{\"name\":\"evnxy\",\"age\":\"21\",\"sex\":\"女\"}]";
NSArray *arr = [str JSONValue];
NSLog(@"%@",arr);