Getting XML values [duplicate] - ios

This question already has answers here:
Parsing XML file with NSXMLParser - getting values
(4 answers)
Closed 7 years ago.
I have an xml which i want to parse it , i succesed to do this but i have a question about the structure of this xml .
<BIBLEBOOK bnumber="1" bname="Genesis" bsname="Gen">
<CHAPTER cnumber="1">
<VERS vnumber="1">At the first God made the heaven and the earth.</VERS>
<VERS vnumber="2">And the earth was waste and without form; and it was dark on the face of the deep: and the Spirit of God was moving on the face of the waters.</VERS>
How can i get the value for bname ?
This is how i parse the xml:
NSString *xmlString = [[NSBundle mainBundle] pathForResource:#"basic_english" ofType:#"xml"];
NSData *xmlData = [NSData dataWithContentsOfFile:xmlString];
NSString* dataStr = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];
NSDictionary *xmlDoc = [NSDictionary dictionaryWithXMLString:dataStr];

Try this. Hope it will help you.
NSString *bname = [xmlDoc valueForKey:#"_bname"];

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 to use plist programmatically? [duplicate]

This question already has answers here:
How do I load a plist file from disk as a NSDictionary on iOS?
(3 answers)
Closed 7 years ago.
I am new in this field. So, can anyone explain how to use plist programmatically? From, where can I start , so that I can learn it properly?
You can access plist as same as NSDictionary. Plist provide data in key value formate.
-you can add your own plist file and use your plist as this way.
NSString *path = [[NSBundle mainBundle] pathForResource: #"plistname" ofType:#"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
NSString *temp = [dict objectForKey:[NSString stringWithFormat:#"%#-Your key name",key]];

Convert NSString to NSUTF32StringEncoding [duplicate]

This question already has answers here:
NSString to treat "regular english alphabets" and characters like emoji or japanese uniformly
(2 answers)
Closed 8 years ago.
I have an NSString that internally uses UTF16 encoding. I want to covert it to use UTF32 , so that
😄 or q both take single index. Currenty 😄 takes 2.
How to do this ?. Even if I can convert to some other type from NSString it will work. Bottom line is to have 😄 or q take equal number of indexes in an array.
Did you try :
NSData *data = [string dataUsingEncoding:NSUTF32StringEncoding];
NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF32StringEncoding];
Have you tried [[NSString alloc]initWithData:encoding:]?
Example would be:
NSData *data = [sourceStr dataUsingEncoding:NSUTF16StringEncoding];
Create your NSUTF32StringEncoded string from the data above
NSString *encodedStr = [[NSString alloc]initWithData:data encoding:NSUTF32StringEncoding];

NSString to NSData conversion [duplicate]

This question already has an answer here:
Converting hex string to hex data
(1 answer)
Closed 9 years ago.
I am having following NSString:
8270be11a217f1420863d76ea7d24820
and i want to convert in following format:
NSData : <8270be11 a217f142 0863d76e a7d24820>
Please help!
Thanks
Try to do this..
NSString* str = #"teststring";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

Encrypted twitter feed

I'm developing an iOS application , that will take a twits from twitter,
I'm using the following API
https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&count=2&screen_name=TareqAlSuwaidan
The problem are feed in Arabic Language ,
i.e the text feed appears like this
\u0623\u0646\u0643 \u0648\u0627\u0647\u0645
How can i get the real text (or how to encode this to get real text) ?
This is not encrypted, it is unicode. The codes 0600 - 06ff is Arabic. NSString handles unicode.
Here is an example:
NSString *string = #"\u0623\u0646\u0643 \u0648\u0627\u0647\u0645";
NSLog(#"string: '%#'", string);
NSLog output:
string: 'أنك واهم'
The only question is exactly what problem are you seeing, are you getting the Arabic text? Are you using NSJSONSerialization to deserialize the JSON? If so there should be no problem.
Here is an example with the question URL (don't use synchronous requests in production code):
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&count=2&screen_name=TareqAlSuwaidan"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSDictionary *object1 = [jsonObject objectAtIndex:0];
NSString *text = [object1 objectForKey:#"text"];
NSLog(#"text: '%#'", text);
NSLog output:
text: '#Naser_Albdya أيدت الثورة السورية منذ بدايتها وارجع لليوتوب واكتب( سوريا السويدان )
Those are Unicode literals. I think all that's needed is to use NSString's stringWithUTF8String: method on the string you have. That should use NSString's native Unicode handling to convert the literals to the actual characters. Example:
NSString *directFromTwitter = [twitterInterface getTweet];
// directFromTwitter contains "\u0623\u0646\u0643 \u0648\u0627\u0647\u0645"
NSString *encodedString = [NSString stringWithUTF8String:[directFromTwitter UTF8String]];
// encodedString contains "أنك واهم", or something like it
The method call inside the conversion call ([directFromTwitter UTF8String]) is to get access to the raw bytes of the string, that are used by stringWithUTF8String. I'm not exactly sure on what those code points come out to, I just relied on Python to do the conversion.

Resources