HTML parsing in iOS development and HTML parsing in iOS development

Source: Internet
Author: User

HTML parsing in iOS development and HTML parsing in iOS development

 

Before parsing, add the following third-party classes to the project:

 

You must add a library for the preceding three classes. The Library is libxml2.2.dylib.

You also need to set some path parameters. In targets, Search for Header Search Paths in build settings and set different values for debug and release;

Set the debug value to/usr/include/libxml2.

Set the value of release to ${SDKROOT}/usr/include/libxml2.

After setting the preceding content, add # import "TFHpple. h" to the class for parsing html, and then write the parsing method;

The general idea is as follows:

1. First, convert the html of the webpage into the NSString data that oc can recognize. The method used is as follows:

NSString * dataString = [NSString stringWithContentsOfURL: [NSURL URLWithString: htmlString] encoding: NSUTF8StringEncoding error: nil]; // htmlString is the URL of an html webpage.

 

The url is the address for saving the html webpage. Select the NSString encoding format based on the data encoding format. If the format is incorrect, an error 261 is returned.

2. truncate the location of the required data and obtain the final resolved field contEnd.

 

 

 

3. Convert dataString to NSData for the TFHpple class

NSData * htmlData = [contEnd dataUsingEncoding: NSUTF8StringEncoding]; // contEnd is the field to be parsed

 

 

 

 

3. Set the nodes in html based on the node values, for example

.....

You can use nodes to set values;

Use htmlData and nodeString to parse the desired values:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.