Objective-C parses the entire HTML (XML) Process

Source: Internet
Author: User
Parse HTML is the same as Parse XML. You can use the parse XML method to parse HTML. nsstring * Path = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent: @ "parsefile.html"]; // obtain the file path nsstring * string = [[nsstring alloc] initwithcontentsoffile: Paths encoding: nsutf8stringencoding error: Nil]; // obtain the file content nserror * error; ddxmldocument * xmldoc = [[ddxmldocument alloc] initwithxmlstring: String options: 0 error: & error]; // convert if (xmldoc) {nsxmlelement * rootelement = [xmldoc rootelement]; // obtain the root node nsxmlelement * bodyelement = [rootelement elementforname: @ "body"]; // obtain the body node nsarray * bodylist = [bodyelement elementsforname: @ "Div"]; // get the DIV node in the body node for (INT textindex = 0; textindex <[bodylist count]; textindex ++) {nsxmlelement * sub_title_list = (nsxmlelement *) [bodylist objectatindex: textindex]; nsstring * headstring = [[[sub_title_list elementforname: @ "h2"] stringvalue]; // obtain the content of the header node, that is, the <H2> label content // if multiple nodes exist, the nsarray * ptstringlist = [sub_title_list elementsforname: @ "p"]; // obtain the P node, that is, the p label for (INT I = 0; I <[textstringlist count]; I ++) {// first method to convert the nsxmlelement type to the nsstring type // nsstring * pstring = @ ""; // pstring = [textstring stringbyappendingstring: [[ptstringlist objectatindex: i] stringvalue]; // The second method converts the nsxmlelement type to nsstring type nsstring * pstring = @ ""; pstring = [textstring stringbyappendingstring: [nsstring stringwithformat: @ "% @", [ptstringlist objectatindex: I] ;}} two parts in the build must be configured in kissxml: 1. click your project (the top column on the left of xcode), click the info button in the center of the top of the xcode window, and click the build Tag: 2. click targets in the left column and double-click the files under it. The Info window of the Project is also opened. Click the build tab:

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.