如何使用AFNetworking 3.0解析XML数据 [英] How can I parse XML data using AFNetworking 3.0
问题描述
我正在做一个简单的项目,使用此链接显示天气报告
,我实现了以下代码来获取结果...
I am doing a simple project to display the weather report using this a link and I implemented the following code to get the result...
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl" parameters:nil progress:nil success:^(NSURLSessionDataTask * task, id responseObject) {
NSData * data = (NSData *)responseObject;
NSLog(@"Response String: %@", [NSString stringWithUTF8String:[data bytes]]);
}failure:^(NSURLSessionDataTask * operation, NSError * error) {
NSLog(@"Error the result is unfound in the database %@", error);
}];
在上面的代码中,我终于检索到XML,但是我不知道如何解析它并使它以表格视图显示...我是ios开发新手,有人可以帮我做什么来获取数据并在表格视图中显示。.
In the above code, I finally retrieved XML but I don't know how to parse it and make it display in table view... I am new to ios developing can anyone please help me what to do to get data and display it in tableview..
推荐答案
每个XML文件(如属性列表)都可以转换为NSDictionary,我是开发人员,但是,如果xml中包含数据,则可以将其转换为NSDictionary
every xml files like property list can be cast as NSDictionary , i am swift developer but , if you have data in xml its possible to cast it to NSDictionary
:
do {
if let plist = try PropertyListSerialization.propertyList(from: plistData,
format: nil)
as? NSDictionary {
// Successfully read list.
print(plist)
} else {
print("not a dictionary")
}
} catch let error {
print("not a plist:", error.localizedDescription)
}
在Objective-C中:
In Objective-C :
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"myPlist.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
在这种情况下,您可以传递数据而不是Plist
in this case you can pass your data instead of Plist
现在您可以将数据缓存到模型中并在UITableView中显示
now you can cache data into your models and show them in your UITableView
好运气;)
这篇关于如何使用AFNetworking 3.0解析XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!