How can I parse XML data using AFNetworking 3.0

大城市里の小女人 提交于 2019-12-25 00:02:33

问题


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);

}];

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..


回答1:


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

in Swift 4 :

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)
}

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];

in this case you can pass your data instead of Plist

now you can cache data into your models and show them in your UITableView

good luck ;)



来源:https://stackoverflow.com/questions/51185846/how-can-i-parse-xml-data-using-afnetworking-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!