使用框架GDataXML-HTML解析XML
1). 先导入框架文件, 然后Command+B编译
Header Search Paths
进行搜索,如下图,点击Basic右侧的All,双击Header Search Paths
,然后将/usr/include/libxml2
加入。加入后再次编译。Other Linker Flags
,然后加入-lxml2
,再次编译发现编译成功。 从网络上获取XML数据
- (void)loadXML {
NSURL *url = [NSURL URLWithString:@"https://127.0.0.1/videos.xml"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:
^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
NSLog(@"连接错误 %@", connectionError);
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200 || httpResponse.statusCode == 304) {
// 解析数据
} else {
NSLog(@"服务器内部错误");
}
}];
}
// 解析数据
// 1. 加载xml文档
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data error:NULL];
// 2. 获取xml文档的根元素
GDataXMLElement *rootElement = document.rootElement;
// 创建可变数组保存对象
NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
// 3. 遍历所有节点
for (GDataXMLElement *element in rootElement.children) {
// 创建对象
Video *v = [[Video alloc] init];
// 4. 把对象加入数组中
[array addObject:v];
// 5. 给对象的属性赋值
for (GDataXMLElement *subElement in element.children) {
[v setValue:subElement.stringValue forKey:subElement.name];
}
// 6. 遍历video的所有属性
for (GDataXMLNode *attr in element.attributes) {
[v setValue:attr.stringValue forKey:attr.name];
}
}
NSLog(@"%@", array);
打印结果
<videos>
<video videoId="1">
<name>01.C语言-语法预览</name>
<length>320</length>
<videoURL>/itcast/videos/01.C语言-语法预览.mp4</videoURL>
<imageURL>/itcast/images/head1.png</imageURL>
<desc>C语言-语法预览</desc>
<teacher>李雷</teacher>
</video>
<video videoId="2">
<name>02.C语言-第一个C程序</name>
<length>2708</length>
<videoURL>/itcast/videos/02.C语言-第一个C程序.mp4</videoURL>
<imageURL>/itcast/images/head2.png</imageURL>
<desc>C语言-第一个C程序</desc>
<teacher>李雷</teacher>
</video>
<video videoId="3">
<name>03.C语言-函数</name>
<length>822</length>
<videoURL>/itcast/videos/03.C语言-函数.mp4</videoURL>
<imageURL>/itcast/images/head3.png</imageURL>
<desc>C语言-函数</desc>
<teacher>韩梅梅</teacher>
</video>
<video videoId="4">
<name>04.C语言-基本数据类型</name>
<length>1113</length>
<videoURL>/itcast/videos/04.C语言-基本数据类型.mp4</videoURL>
<imageURL>/itcast/images/head4.png</imageURL>
<desc>C语言-基本数据类型</desc>
<teacher>韩梅梅</teacher>
</video>
<video videoId="5">
<name>05.C语言-基本运算</name>
<length>594</length>
<videoURL>/itcast/videos/05.C语言-基本运算.mp4</videoURL>
<imageURL>/itcast/images/head5.png</imageURL>
<desc>C语言-基本运算</desc>
<teacher>韩梅梅</teacher>
</video>
<video videoId="6">
<name>06.C语言-数组</name>
<length>923</length>
<videoURL>/itcast/videos/06.C语言-数组.mp4</videoURL>
<imageURL>/itcast/images/head6.png</imageURL>
<desc>C语言-数组</desc>
<teacher>韩梅梅</teacher>
</video>
<video videoId="7">
<name>07.C语言-字符串</name>
<length>848</length>
<videoURL>/itcast/videos/07.C语言-字符串.mp4</videoURL>
<imageURL>/itcast/images/head7.png</imageURL>
<desc>C语言-字符串</desc>
<teacher>韩梅梅</teacher>
</video>
<video videoId="8">
<name>08.C语言-指针</name>
<length>502</length>
<videoURL>/itcast/videos/08.C语言-指针.mp4</videoURL>
<imageURL>/itcast/images/head8.png</imageURL>
<desc>C语言-指针</desc>
<teacher>韩梅梅</teacher>
</video>
<video videoId="9">
<name>09.C语言-指针用例</name>
<length>558</length>
<videoURL>/itcast/videos/09.C语言-指针用例.mp4</videoURL>
<imageURL>/itcast/images/head1.png</imageURL>
<desc>C语言-指针用例</desc>
<teacher>韩梅梅</teacher>
</video>
<video videoId="10">
<name>10.C语言-指针与数组</name>
<length>1682</length>
<videoURL>/itcast/videos/10.C语言-指针与数组.mp4</videoURL>
<imageURL>/itcast/images/head2.png</imageURL>
<desc>C语言-指针与数组</desc>
<teacher>韩梅梅</teacher>
</video>
<video videoId="11">
<name>11.C语言-指针与字符串</name>
<length>1092</length>
<videoURL>/itcast/videos/11.C语言-指针与字符串.mp4</videoURL>
<imageURL>/itcast/images/head3.png</imageURL>
<desc>C语言-指针与字符串</desc>
<teacher>韩梅梅</teacher>
</video>
</videos>