Parse HTML code in objective-C

Source: Internet
Author: User

From: http://www.cocoachina.com/macdev/objc/2010/1104/2296.html

This section parses the htmlCodeShared by cocoachina member "Yuehua Water", we hope to help developers

# Import "stringchuli. H "/* Project role: link network resolution HTML */@ implementation stringchuli // access the webpage source code-(nsstring *) urlstring :( nsstring *) value {nsurl * url = [nsurl urlwithstring: value]; nsdata * Data = [nsdata datawithcontentsofurl: url]; // solves Chinese garbled characters and uses gbknsstringencoding ENC = encode (encoding ); nsstring * retstr = [[nsstring alloc] initwithdata: data encoding: ENC]; return retstr ;}/ * function: intercept the string STR from value1 to value2: string value1 to be processed: matching string value2 on the left: matching string on the right */-(nsstring *) STR :( nsstring *) STR value1 :( nsstring *) value1 value2 :( nsstring *) value2 {// I: subscript int I of the string matching on the left in STR; // J: subscript Int J of the string matching on the right in str1; // This class can use value1 to match the string nsange range1 = [STR rangeofstring: value1]; // you can determine whether range1 matches the string if (range1.length> 0) {// convert it to nsstringnsstring * result1 = nsstringfromrange (range1); I = [self indexbyvalue: result1]; // cause: add the length of the matched string to obtain the correct subscript I = I + [value1 length];} // Delete the nsstring * str1 = [STR substringfromindex: i]; nsange range2 = [str1 rangeofstring: value2]; If (range2.length> 0) {nsstring * result2 = nsstringfromrange (range2); j = [self indexbyvalue: result2];} nsstring * str2 = [str1 substringtoindex: J]; return str2;} // subscript of the matched information obtained by filtering-(INT) indexbyvalue :( nsstring *) STR {// use the nsmutablestring class, which can append nsmutablestring * value = [[nsmutablestring alloc] initwithformat: @ ""]; nsstring * colum2 = @""; int J = 0; // print out the lower mark value for (INT I = 1; I <[STR length]; I ++) {nsstring * colum1 = [STR substringfromindex: i]; [value appendstring: colum2]; colum2 = [colum1 substringtoindex: 1]; If ([colum2 is1_tostring: @ ","]) {J = [value intvalue]; break ;}} [value release]; return J ;}@ end
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.