Parsing a xml file in afnetworking and retrieve data doesn't work - xml-parsing

With this url I tried but it doesn't work.
Can someone help me to give simple xml parsing code and retrive data from this url?
http://dynamic.feedsportal.com/pf/555218/http://toi.timesofindia.indiatimes.com/rssfeedstopstories.cms
I tried but only reached at
NSData * data1 = (NSData *)responseObject;
NSString *fetchedXML = [NSString stringWithUTF8String:[data1 bytes]];
NSLog(#" xml data :: %#", fetchedXML);
then I can't understand how to get data

NSXMLParser is a standard way to parse xml. I suggest you to use XMLDictionary which is more handy wrapper of NSXMLParser.

Related

Getting null from NSDictionary when NSJSONSerialization is being used

I am new to developing and stackoverflow please help me. i am trying to do a simple application where the YQL link is used to get local data and display it in table format. For that i am converting the data into dictionary , later i want to send it into table. But when i tried to convert data to Dictionary it says null. Please help me. Check the Screenshot below. Thanks in advance.
NSString *str = #"https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20zip%3D'94085'%20and%20query%3D'pizza'&diagnostics=true";
Here i took json query into a string (*str)
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
// NSString *stringFromData = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(#"%#", stringFromData);
When i tried to implement this commented code im getting the result as expected, but i want to put all the data into dictionary and display it, so i tried to convert the data into dictionary
NSDictionary *dataFromWeb = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSDictionary *queryDict = [dataFromWeb objectForKey:#"query"];
NSDictionary *results = [dataFromWeb objectForKey:#"results"];
NSString *allResults = [queryDict objectForKey:#"Results"];
NSLog(#"%#", dataFromWeb);
}
The response returning from Yahoo API is XML by default. You should append format=json to the querystring in order to get the response in JSON format so you can parse it using NSJSONSerialization class:
https://query.yahooapis.com/v1/public/yql?format=json&q=select...
Your Api Return xml data so you need to do xml parsing
NSJSONSerialization is used for json parsing
NSString *str = #"https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20zip%3D'94085'%20and%20query%3D'pizza'&diagnostics=true";
NSURL *UrlFromStr = [NSURL URLWithString:str];
hit this UrlFromStr on your browser you see it return xml data not json
use NSXMLParser to parse xml data
Please Use NSXMLParser To Parse xml data instead of NSJSONSerialization.
NSJSONSerialization is used to parse JSON data.
Declare in .h file
#property (nonatomic, strong) NSXMLParser *xmlParser;
in .m file.
NSString *str = #"https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20local.search%20where%20zip%3D'94085'%20and%20query%3D'pizza'&diagnostics=true";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
self.xmlParser = [[NSXMLParser alloc] initWithData:data];
self.xmlParser.delegate = self;
// Initialize the mutable string that we'll use during parsing.
self.foundValue = [[NSMutableString alloc] init];
// Start parsing.
[self.xmlParser parse];
The format of objects you wanna parse is XML,but you use the class of ParseJSON to parse it ,so it return NULL.You can use the class of NSXML to parse it and then set its delegate to execute the related methods...Good luck to you ..
your Api is not a json its XML format use the XML Parser, visit below link,
http://www.theappguruz.com/blog/xmlparsing-with-nsxmlparser-tutorial
hope its helpful.

How can i decode the HTML Code in iOS?

I am using below code to Decode the HTML code:
-(NSString *)decodeString:(NSString *)str
{
// To Remove the HTML code to Nsstring
NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *options = #{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}; NSAttributedString *decodedString;
decodedString = [[NSAttributedString alloc] initWithData:stringData options:options documentAttributes:NULL error:NULL];
return decodedString.string;
}
This code takes a long time to decode the string. So is there is any other way to decode the HTML code?
I don’t want to use the async OR GCD, because I am using this code at the time of stoaring the records in the database.
So please suggest some alternative. Thanks in advance.
If you are storing records in the database then you can use background queue with and Loader or HUD.Because as much data you will decode it will take more time.So in my opinion please make it in background

Unwanted XML header string coming in JSON response in iOS

The problem I am facing is, when I am fetching JSON data from the server, XML header string comes along with it. I've tried some code changes, but in vain. The response data is perfect but the problem is the XML header. So as of now, I tried substring on the response string and managed to pull out the proper JSON data. But what I want to know is the reason behind this problem and what am I doing wrong here.
This is my JSON retrieving code.
NSURL *url=[NSURL URLWithString:#"myServiceURL"];
NSData *response = [NSData dataWithContentsOfURL:url];
NSString *badStr = [NSString stringWithUTF8String:[response bytes]];
NSString *goodStr = [badStr substringFromIndex:76];
NSString *finalStr = [goodStr substringToIndex:[goodStr length]-9];
NSData *goodData = [finalStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:goodData options: NSJSONReadingMutableContainers error: &error];
This includes the code for performing substring on the received JSON response.
And here is a sample JSON response that I am receiving.
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">
[{"Title":"DemoTitle","CreationDate":"06/06/2014","Description":"DemoDescription"}]
</string>
Need Guidance. Thanks.
If for whatever reason you can't get the server to stop sending the data in xml with json embedded then you can parse the XML first with NSXMLParser and then parse the JSON from that.

Get XML's info using arrayWithContentsOfFile

I'm trying to get XML's info by using NSFileManager and fill content of array by arrayWithContentsOfFile method.
The problem is when I print this array, it will appears as 'null'. I'm thinking about an encoding problem, but not sure...?
My code :
NSString *evalPath = [NSString stringWithFormat:#"%#/eval_list.xml", [UIAppDelegate documentsPath]];
NSArray *evalList = [[NSArray alloc] init];
if ([[NSFileManager defaultManager] fileExistsAtPath:evalPath]) {
evalList = [NSArray arrayWithContentsOfFile:evalPath];
NSLog(#"Content of Array : %#", evalList);
if (evalList && ([evalList count]))
evalList = [UIAppDelegate postProcessEvalList:evalList];
}
But :
NSLog(#"Content of Array : %#", evalList);
Return 'null'. Can someone help me ?
Thanks
You cant convert xml directly to array like that. Rather you will have to parse the xml by yourself. Use stringWithContentsOfFile: to fetch the content into string and then convert it to xml using NSXMLParser and convert it to array by yourself.
See this link for details http://iosbala.blogspot.fi/2013/04/how-to-using-nsxmlparser-in-iphone-sdk.html on how to parse xml.

NSJSONSerialization from NSString

Is it possible if I have a NSString and I want to use NSJSONSerialization? How do I do this?
First you will need to convert your NSString to NSData by doing the following
NSData *data = [stringData dataUsingEncoding:NSUTF8StringEncoding];
then simply use the JSONObjectWithData method to convert it to JSON
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
You need to convert your NSString to NSData, at that point you can use the +[NSJSONSerialization JSONObjectWithData:options:error:] method.
NSString * jsonString = YOUR_STRING;
NSData * data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError * error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (!json) {
// handle error
}
You can convert your string to NSData by saying:
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
You can then use it with NSJSONSerialization. Note however that NSJSONSerialization is iOS5 only, so you might be better off using a library like TouchJSON or JSONKit, both of which let you work directly with strings anyway, saving you the step of converting to NSData.
I wrote a blog post that demonstrates how to wrap the native iOS JSON class in a general protocol together with an implementation that use the native iOS JSON class.
This approach makes it a lot easier to use the native functionality and reduces the amount of code you have to write. Furthermore, it makes it a lot easier to switch out the native implementation with, say, JSONKit, if the native one would prove to be insufficient.
http://danielsaidi.com/blog/2012/07/04/json-in-ios
The blog post contains all the code you need. Just copy / paste :)
Hope it helps!

Resources