How to use objective-C to parse HTML and XML

Source: Internet
Author: User

 

Use objective-C to parse HTML or XML. The system uses either libxml or nsxmlparser. However, both of these methods require writing a lot of code to process captured content, which is not intuitive.

There is a better class library, hpple, which is a lightweight packaging framework that can solve this problem well. It uses XPath to locate and parse HTML or XML.

 

Installation steps:

-Add libxml2 to your project.
Menu project-> edit Project Settings
Search for "header search paths"
Add a new search path "$ {sdkroot}/usr/include/libxml2 ″
Enable recursive Option

 

-Add libxml2 library to your project
Menu project-> edit Project Settings
Search for "Other linker flags"
Add a new search flag "-lxml2 ″

 

-Add the following hpple source code to your project:
Htfpple. h
Htfpple. m
Htfppleelement. h
Htfppleelement. m
Xpathquery. h
Xpathquery. m

 

-XPath learning address http://www.w3schools.com/XPath/default.asp

 

Sample Code:

 

<Br/> # import "tfhpple. H "</P> <p> nsdata * Data = [[nsdata alloc] initwithcontentsoffile: @" example.html "]; </P> <p> // create parser <br/> xpathparser = [[tfhpple alloc] initwithhtmldata: Data]; </P> <p> // get all the cells of the 2nd row of the 3rd table <br/> nsarray * elements = [xpathparser search: @ "// table [3]/TR [2]/TD"]; </P> <p> // access the first cell <br/> tfhppleelement * element = [elements objectatindex: 0]; </P> <p> // get the text within the cell tag <br/> nsstring * content = [element content]; </P> <p> [xpathparser release]; <br/> [data release]; <br/> 

 

In addition, there is a similar solution for Reference
Elementparser http://github.com/Objective3/ElementParser

 

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.