//
// xmlController.m
// XML
//
// Created by 0卜7r从l on 13-11-26.
// Copyright (c) 2013年 0卜7r从l. All rights reserved.
//
#import "xmlController.h"
@interface xmlController ()
@end
@implementation xmlController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.stuMuArray=[[NSMutableArray alloc]init];
[_stuMuArray release];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_stuMuArray release];
[_xmlSystem release];
[_Dom release];
[_json release];
[_jsonkit release];
[super dealloc];
}
//jsonkit 解析
- (IBAction)jsonKit:(id)sender {
//获取路径
NSString* path=[[NSBundle mainBundle]pathForResource:@"student.json" ofType:Nil];
NSData *data=[NSData dataWithContentsOfFile:path];
NSArray* array=[data objectFromJSONData];
[array JSONData];
}
- (IBAction)xmlSystemAction:(id)sender {
//获取路径
NSString* path=[[NSBundle mainBundle]pathForResource:@"Student.xml" ofType:Nil];
NSData *data=[NSData dataWithContentsOfFile:path];
NSXMLParser *parser=[[NSXMLParser alloc]initWithData:data];
parser.delegate=self;
[parser parse];
}
/**
* 解析器找到开始标签
*
* @param parser 解析器对象
* @param elementName 标签名字
* @param namespaceURI 命名空间
* @param qName 添了前缀的标签
* @param attributeDict 属性
*/
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@"~|~~~%@",elementName);
}
//找到结束点
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"~?~~~%@",elementName);
}
//取值
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@"~value~~~%@",string);
}
//dom解析
- (IBAction)DomAction:(id)sender {
//获取路径
NSString* path=[[NSBundle mainBundle]pathForResource:@"Student.xml" ofType:Nil];
NSData *data=[NSData dataWithContentsOfFile:path];
GDataXMLDocument *xmlData=[[GDataXMLDocument alloc]initWithData:data options:0 error:Nil];
GDataXMLElement *root=[xmlData rootElement];
NSLog(@"~~~~~~%@",root);
//找到某个节点下得所有某个子节点
NSArray * stuArray=[root elementsForName:@"student"];
NSLog(@"!!!!!%@",stuArray);
for (GDataXMLElement *dic in stuArray) {
Student *stu=[[Student alloc]init];
//取属性
GDataXMLNode *node=[dic attributeForName:@"type"];
NSString* value1=[node stringValue];
NSLog(@"~~~~~~~~~~~~~~%@",value1);
NSArray *names=[dic elementsForName:@"name"];
GDataXMLElement *name=[names objectAtIndex:0];
NSArray *sexs=[dic elementsForName:@"sex"];
GDataXMLElement *sex=[sexs objectAtIndex:0];
NSArray *numbers=[dic elementsForName:@"number"];
GDataXMLElement *number=[numbers objectAtIndex:0];
stu.name=name.stringValue;
stu.sex=sex.stringValue;
stu.number=[number.stringValue intValue];
[self.stuMuArray addObject:stu];
[stu release];
}
//xpath
NSArray *nameArray=[root nodesForXPath:@"//name" error:Nil];
NSLog(@"***************======%@",nameArray);
//打印stu数组
for (Student *stu in _stuMuArray) {
NSLog(@"数组::::%@",stu.name);
}
}
//解析json
- (IBAction)jsonAction:(id)sender {
//获取路径
NSString* path=[[NSBundle mainBundle]pathForResource:@"student.json" ofType:Nil];
NSData *data=[NSData dataWithContentsOfFile:path];
NSArray *array=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:Nil];
//id 转json
NSData *dataJson=[NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:Nil];
NSLog(@"%@",array);
}
@end