Detailed description of XML in non-UTF-8 for iPhone applications

Source: Internet
Author: User

IPhoneNon-Beijing applicationsUTF-8InXMLIs the content to be introduced in this article, mainly introducesIphoneInUTF-8OfXMLApplication. Let's take a look at the details. DoIPhoneNetwork Application, processingXMLNSXMLParser is easy to use. However, this is notUTF-8, Parsing will fail. This article is intended to share with you some new developments in this area.

In an RSS address, we can get the followingXMLFile. I just want to take a paragraph here,

 
 
  1. <? Xml version = "1.0" encoding = "big5"?>
  2. & Lt; rss version = "2.0" & gt;
  3. <Channel>
  4. <Title> RTHK On Internet-immediate new release </title>
  5. <Link> <! [CDATA [http://www.rthk.org.hk/rthk/news/expressnews/#]> </link>
  6. <Description> RTHK On Internet-instant new versions </description>
  7. <PubDate> Sun, 11 Oct 2009 15:02:02 + 0800 </pubDate>
  8. <Item>
  9. <Title> <! [CDATA [& #34910; the department of health requires the Baptist Medical College to submit a statement about the death case in the production industry within four weeks]> </title>
  10. <Link> <! [CDATA [links> </link>
  11. <Description> <! [CDATA [

& #34910; The Department of Health has received a notice from the Baptist community court last night that there is a production death case, and it will take four weeks for the Baptist community to reach the site. & #34910; the department submits an appeal and the case is also submitted to the death court.

& #34910; data records of the Department, from to, received a total of 97 communications related to primary school accidents. Among the accidents that occurred last year, the most were the cases of concurrent or interventional procedures, followed by pregnancy and production deaths, or a pregnancy occurs during childbirth, at birth, or after birth; some of them include the birth of a child with death or secondary birth; surgical or interventional manual procedures are performed for the patient or body.

 
 
  1. ]]></description> 
  2. <pubDate> 
  3. Sun, 11 Oct 2009 14:50:46 +0800  
  4. </pubDate> 
  5. </item> 
  6. </channel> 
  7. </rss> 

This is a commonly used XML returned by RSS, which is in Traditional Chinese and the encoding format is big5. The encoding format of big5 has two meanings:

1) NSData returned by a network stream is big5 encoded. Therefore, NSData sent to NSXMLParser cannot be correctly parsed.

2) The first sentence indicates that the XML file is also encoded in big5.

My solution is as follows:

1) convert NSData of Big5 encoding into NSData of UTF-8 Encoding

2) Change the first line <? Xml version = "1.0" encoding = "big5"?> Convert to <? Xml version = "1.0" encoding = "UTF-8"?>

The second conversion is not difficult, as long as we have an NSString object. CFStringRef is used for the first conversion. The Code is as follows:

 
 
  1. CFStringRef big5Str = CFStringCreateWithBytes(NULL,     
  2.                                     [inData bytes],     
  3.                                     [inData length],     
  4.                                     kCFStringEncodingBig5_HKSCS_1999,     
  5.                                     false); //[A]     
  6. if (NULL == big5Str) {     
  7.         return nil;     
  8. }     
  9. else {     
  10.     NSString *big5NSString = (NSString *)big5Str;     
  11.     NSString *utf8NSString = [big5NSString stringByReplacingOccurrencesOfString:@"<?xml version=\"1.0\" encoding=\"big5\"?>"      
  12.                            withString:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"]; //[B]     
  13.     return [utf8NSString dataUsingEncoding:NSUTF8StringEncoding]; //[C]     
  14. }    
  15.  
  16. CFStringRef big5Str = CFStringCreateWithBytes(NULL,  
  17.          [inData bytes],  
  18.          [inData length],  
  19.          kCFStringEncodingBig5_HKSCS_1999,  
  20.          false); //[A]  
  21. if (NULL == big5Str) {  
  22.         return nil;  
  23. }  
  24. else {  
  25.  NSString *big5NSString = (NSString *)big5Str;  
  26.  NSString *utf8NSString = [big5NSString stringByReplacingOccurrencesOfString:@"<?xml version=\"1.0\" encoding=\"big5\"?>"   
  27.          withString:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"]; //[B]  
  28.  return [utf8NSString dataUsingEncoding:NSUTF8StringEncoding]; //[C]  
  29. }  

The idea is to first convert NSData into CFStringRef object [A], and then this object into NSData [C] of the UTF-8, which solves the problem [1]

[B] solves the problem [2]. Pay attention to the following two issues:

1) if your XML code is GBK, GB23120, or another one, kCFStringEncodingBig5_HKSCS_1999 must be replaced with your corresponding encoding method.

(2) If your XML code is big5, maybe kCFStringEncodingBig5_HKSCS_1999 is not suitable for your application. Because there are two encoding constants corresponding to big5, they are:

 
 
  1. kCFStringEncodingBig5_E  
  2. kCFStringEncodingBig5。 

You can refer to the help documentation and try it one by one.

Then, the NSData of return is sent to NSXMLParser, which can be parsed correctly. But it's not over yet. Let's take a closer look at the XML file and see what's like & #34910. This is the word "success" in Traditional Chinese. If we don't handle this, we will show it to users as & #34910, which is obviously not acceptable. You can use the following sentence to convert a number into an NSString:

 
 
  1. [NSString stringWithFormat:@"%C", number] 

This number is 34910, an integer, And the decimal integer is not hexadecimal ). Here, the entire processing process is OK, and we can get the RSS news of Big5 correctly. This is a customer project, but I can show it to everyone ,:

Summary: DetailsIPhoneNon-Beijing applicationsUTF-8InXMLI hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.