问题
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