iOS -数据库网络之xml解析之第三方解析XML

时间:2023-03-09 19:11:12
iOS -数据库网络之xml解析之第三方解析XML
1.导入第三方插件(GDalaXMLNode)
 
2.第三方插件配置
 
libxml/tree.h 路径
 
在项目属性中--Bulid Settings中搜索 Search
--Search Paths 中--Headr Search Paths 添加路径(/usr/include/libxml2)
 
非ARC
 
在项目属性中--Bulid Phases
--Compile Sources 中--GDataXMLNode.m 添加参数(-fno-objc-arc)
 
第三方库文件加载
 
在项目属性中--Bulid Phases--Link Binary With Libraies--libxml2 文件
 
3.使用第三方插件
 
//设置URL
 
NSURL * url=[NSURL URLwithString:@"XML远程路径"];
 
//会话
NSURLSession * session=[NSURLSession sharedSession];
//任务
NSURLSessionDataTask * task=[session dataTaskWithURL:url completionHandler:^(){
}];
 
//任务开始
 
[task resume];
 
//数据安全配置
 
info.plist---OPen As--Source Code
 
添加
 
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
 
导入第三方库
 
#import "GDataXMLNode.h"
 
基于文档的解析
 
//创建一个文档
 
GDataXMLDocument * doc =[[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
 
if(doc){
 
//拿到根元素
 
GDataXmLElement *rootElement=doc.rootElement;
 
//快速遍历
 
for(GDataXmLElement * student in rootElement.children){
 
for(GDataXmLElement * property in student.children){
 
NSLog(@"%@,%@", property.name,proparey.stringValue):
}
 
}
 
}else{
 
}