标签:
XML
1.定义
<?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]); }
标签:
原文地址:http://www.cnblogs.com/3WWanXiang/p/4905857.html