The processing method of xml-gb2312 Web page encoding encountered

Source: Internet
Author: User

1. Convert the requested data to

2. take the first nine bytes of an XML file

nsdata * XMLData = [self. Data subdatawithrange:nsmakerange(0,+)];

3. decoding with UTF-8 encoding

nsstring *xmlstr = [[nsstring alloc] initwithdata: XMLData encoding: Nsutf8stringencoding];

// search GB2312, if found, change GB2312 to Utf-8 (PS: replacement string)

if ([Xmlstr rangeofstring:@ "\" gb2312\ "" options:nscaseinsensitivesearch] . Location! = nsnotfound)

{

NSLog(@ " encountered:GB2312 encoding founded. is changing GB2312 to utf-8 ");

nsstringencoding enc = cfstringconvertencodingtonsstringencoding(kcfstringencodinggb_18030_ );

nsstring *utf8str = [[nsstring alloc] initwithdata:self. Data encoding: enc];

UTF8STR = [Utf8str stringbyreplacingoccurrencesofstring:@ "\" gb2312\ "" withstring:@ "\" Utf-8\ "" options:nscaseinsensitivesearch range:nsmakerange(0 ,Max)];

New data that can be parsed properly

nsdata *newdata = [utf8str datausingencoding:nsutf8stringencoding];

}

The processing method of xml-gb2312 Web page encoding encountered

Related Article

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.