objective c string encoding issue - iphone

My app receives some data from server which is in XML format and some tags contains Japanese words. When I tried to execute the link in the browser, it gets the correct response in Japanese. For example:
<root>
<categorylist>
<cid>19</cid>
<cname>ファミリー</cname>
<lang>jap</lang>
</categorylist>
</root>
..but in my app, the response string is in some encoded format like the following:
<root>
<categorylist>
<cid>19</cid>
<cname>ファミリー</cname>
<lang>jap</lang>
</categorylist>
</root>
My code to receive XML from the server is:
NSString *req=#"http://www.myserver.com/category.php?lang=jap";
NSString *resultString=[NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:nil];
Is there a problem with my code? How can I convert the response to correct Japanese strings?

Try looking at some of the decoding methods listed on this question: HTML character decoding in Objective-C / Cocoa Touch

Related

iOS - Serialize JSON with arabic NSString

I'm in quite a pickle, I'm acquiring user input in Arabic which is stored in a NSString.
The string is displayed properly both in the app and the debug window.
Here's my problem: the string is added to a NSMutableDictionary then serialized to JSON
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
options:NSJSONWritingPrettyPrinted
error:&error];
The data is then POSTed to the server, but the once Arabic content is represented as ????
This is obviously an encoding issue. I can't find the proper method to encode my NSString with UTF-8 and still add it to the Dictionary.
AddingPercentEncoding will encode it as URL UTF-8 encoding which is not what I'm after.
The only reasonable encoding I find is [NSString UTF8String] which turns the string to const char * that is not an NSObject.
What I'm really after is: مرحبا UTF-8 ---> Ùرحبا
The decoding works perfectly, if the iOS app receives JSON with such representation, the resulting NSDictionary will hold a key that returns the proper value in Arabic.
Thank you for your help! :D

SOAP request response giving undesired result

I am trying to fetch response from SOAP request.Response is an xml data.When i try to convert data into String it shows <,> symbols as '&gt' and '&lt' with ';' at end.Then the xml parser is not able to parse it
Check out this NSString category for HTML.
It adds several functions to NSString which you may find useful, like:
- (NSString *)stringByDecodingHTMLEntities;
You can run this function on your string and decode the HTML entities before giving the string to the XML parser.

Xml Parsing on iPhone

I have checked lots of tutorials on xml parsing in iOS ..
but most of them have already set the xml response from the server in a clear understandable line by line format
<?xml version="1.0" encoding="utf-8"?>
<people> <person>
<firstName>James</firstName> <lastName>Franklin</lastName>
</person>
<person>
<firstName>Snidely</firstName>
<lastName>Wilsonn</lastName>
</person>
<person>
<firstName>Dudley</firstName>
<lastName>Doright</lastName>
</person> </people>
Now seeing this.. it is pretty easy to follow what tags you have to filter and extract data from ..
But now when i am doing this . (like this in my app)
NSError *error;
NSData *linkData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"someURL"] options:NSDataReadingUncached error:&error]
NSString *stringData = [[NSString alloc]initWithData:linkData encoding:NSUTF8StringEncoding];
NSLog(#" Data /n/n/n %#",stringData);
It is coming like this in console :
Data /n/n/n {"entries":[{"id":"37504814","title":"Live Blog: Patrik Schumacher","created":"2012-02-09 23:43:00","updated":"2012-02-10 03:34:05","user_id":"15084","entry_type":"20","location":null,"portfolio_id":null,"arc_name":"Lian Chikako Chang","parent_image":"xyz2sb7y9n4mtvbb.jpg","link":"http:live-blog-patrik-schumacher","quote":null,"content":" \n<p>\n\tHi Archinect!<\/p>\n<p>\n\tPatrik Schumacher, partner at Zaha Hadid Architects and founding director at the AA Design Research Lab, is in Piper tonight for a lecture on \"Parametric Order: 21st century architectural order.\"<\/p>\n<p>\n\t<img alt=\"\" src=\"http
NOw this is JSON and XML both .. but there are thousands of lines more after it /...and i am not able to understand it as it is...any way to get it in the proper formatted format like the above given XML.??
ALSO one more question : Is the XML parser best for parsing or should i do it with other projects for xml parsing on github?.
Thanks
this will format xml the xml for you to make it more readable.
http://xmltoolbox.appspot.com/
What you get is JSON string with HTML tags embedded. (Is it intended?. or the response is corrupt).
You can't parse the HTML with XML Parser.
NSXMLParser is a SAX parser. It is good for large amount of data.
I am unable to understand your question. But you need to take look at following tutorials. You really need it :
How To Choose the Best XML Parser for your iPhone Project
How to Read and Write XML Documents with GDataXML
How to Translate Text with Google Translate and JSON on the iPhone
It will really help you.
Your response content is in JSON format.And in turn it contains HTML data for the key : **content**.
NSXmlParser is both DOM and SAX parser. But the selection of the parser depends upon various factors. You can refer to this blog for how to select a XML Parser.

iphone's nsxmlparser parsing RSS causes encoding problems

Im working on simle RSS reader. This reader loads data from internet via this code:
NSXMLParser *rss = [[NSXMLParser alloc] initWithURL:[NSURL URLWithString:#"http://twitter.com/statuses/user_timeline/50405236.rss"]];
My problem is with encoding. RSS 2.0 file is supposed to be UTF8 encoded according to encoding attribute in XML file.
<?xml version="1.0" encoding="utf-8"?>
So when I download URLs content I get text truncated after first occurance of char with diacritics, example: ľ š č ť ž ý á í é, etc.
I tried to solve the problem by downloading URL as UTF8 string, I used this code:
NSString *rssXmlString = [NSString stringWithContentsOfURL: [NSURL URLWithString: #"http://www.macblog.sk/rss.xml"] encoding:NSUTF8StringEncoding error: nil];
NSData *rssXmlData = [rssXmlString dataUsingEncoding: NSUTF8StringEncoding];
Did not help. Thanx for your responses.
Check out MWFeedParser on GitHub, it's an open source RSS/Atom feed parser I've released and it makes reading and parsing web feeds extremely easy.
There's a simple demo app too which shows how easy it is to implement.
Hope this might be of some use!

NSXMLParser init with XML in NSString format

I just ran into this one and couldn't seem to get any clear answer from the documentation.
Im retrieving some XML through a HTTPS connection. I do all sorts of authentication etc. so I have a set of classes that deals with this in a nice threaded way.
The result is an NSString that goes something like:
<response>
//some XML formatted test
</response>
This means that there is no encoding="UTF-8" indent="yes" method="xml" or other header blocks to indicate that this is actual XML and not just an NSString.
I guess I will use [NSXMLParser initWithData:NSData] to construct the parser, but how will I format or cast my NSString of xml formatted text into a proper NSData object that NSXMLParser will understand and parse?
Hope it makes sense, thank you for any help given :)
You can convert a string to a NSData object using the dataUsingEncoding method:
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
You can then feed this to NSXMLParser.
The headers are optional, but you can insert the appropriate headers yourself if needed:
NSString *header = #"<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
NSString *xml = [NSString stringWithFormat:#"%#\n%#", header, response);
NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
NSXMLParser *praser = [NSXMLParser initWithData:data];
By the time NSXMLParser has the data it's pretty much going to be expecting it to be XML ;-)
I'm pretty sure the processing instruction header is optional in this context. The way you get the NSString into the NSData is going to dictate the encoding (using dataUsingEncoding:).
(edit: I was looking for the encoding enum, but Philippe Leybaert beat me to it, but to repeat it here anyway, something like: [nsString dataUsingEncoding:NSUTF8StringEncoding])
I've passed NSStrings of XML in this way before with no issues.
Not specific to this question as such, but on the subject of XML parsing in an iPhone context in general you may find this blog entry of mine interesting, too.