1.引用类库libxml2
2.build setting -> Header search Paths 下
添加 :/usr/include/libxml2
Other Linker Flags 下
添加: -lxml2
3.GDataXMLNode 是在MRC下写的, 要想在ARC环境下使用需要做一些操作
build Prases下 搜索 GData 添加 -fno-objc-arc
- 启动Apache的命令
sudo apachectl -k restart 重启Apache
sudo apachectl -k start 启动Apache
sudo apachectl -k stop 停止Apache
-(void)request{
//请求数据
//1.创建NSURL
NSURL * url = [NSURL URLWithString:@“http://127.0.0.1/student.xml”];
_totalData = [[NSMutableData alloc]init];
//创建请求
NSURLRequest * request = [NSURLRequest requestWithURL:url];
//创建连接 发送请求
[NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark – NSURLConnection 协议方法
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
//会多次被调用
[_totalData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//完成了数据请求
//解析数据 二进制
NSXMLParser * parser = [[NSXMLParser alloc]initWithData:_totalData];
//设置代理
parser.delegate = self;
//开始解析
[parser parse];
//清空所有的数据
[_totalData setLength:0];
}
NSData *data = [[NSData alloc]initWithContentsOfURL:url];
//创建解析对象Gdata(主要负责我们文件内容的读取 自动分析数据结构)
GDataXMLDocument *document=[[GDataXMLDocument alloc]initWithData:data options:0 error:nil];
//取到根节点
GDataXMLElement *root = [document rootElement];
//查找子节点
NSArray *classElements = [root elementsForName:@“class”];
for (GDataXMLElement * element in classElements) {
NSString *s = [[element attributeForName:@“c_name”] stringValue];
[self.titleArray addObject:s];
NSArray * studentElements = [element elementsForName:@“student”];
_array = [[NSMutableArray alloc]init];
for (GDataXMLElement * elem in studentElements) {
student *s=[[student alloc]init];
s.name=[[[elem elementsForName:@“name”]firstObject ]stringValue];
s.age=[[[elem elementsForName:@“age”]firstObject ]stringValue];
[self.array addObject:s];
}
[self.arr addObject:self.array];
}
[self.table reloadData];