开发者

How to get HTML elements using objective C for iphone dev

开发者 https://www.devze.com 2023-02-11 14:56 出处:网络
Right now I am working on a small project which requires me to pull some table from a website. I read some posts which suggests using NSXMLParser and some have suggested libxml2.2.dylib. Which of thes

Right now I am working on a small project which requires me to pull some table from a website. I read some posts which suggests using NSXMLParser and some have suggested libxml2.2.dylib. Which of these would be easier to use? I only need to get a few values from the page, nothing too co开发者_JAVA百科mplicated.


NSXMLParser; especially if your needs aren't that deep. There are also some external libraries to consider: TouchXML.


I'm also parsing HTML in my current project and i use GDataXML for that which uses libxml2 underneath. It lets you use XPath and in my case it reduces the parsing code to just a few lines and i prefer it over NSXMLParser. Depending on the performance you need XPath might not be the best option for you - in my case it's fast enough though.

A brief example on how you would get all tables out of the body of your html:

GDataXMLDocument* xmlDocument = [[GDataXMLDocument alloc] initWithXMLString:htmlString options:0 error:nil];
NSArray* tablesInBody = [xmlDocument.rootElement nodesForXPath:@"body/table" error:nil]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号