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: