Git Product home page Git Product logo

hpple's Introduction

DESCRIPTION

Hpple: A nice Objective-C wrapper on the XPathQuery library for parsing HTML.

Inspired by why the lucky stiff's Hpricot.

AUTHORS

FEATURES

  • Easy searching by XPath (CSS selectors are planned)
  • Parses HTML (XML coming soon)
  • Easy access to tag content, name, and attributes.

INSTALLATION

  • Open your XCode project and the Hpple project.
  • Drag the "Hpple" directory to your project.
  • Add the libxml2.dylib framework to your project and header search path as described at Cocoa with Love

USAGE

#import "TFHpple.h"

NSString * pathPath = [[NSBundle mainBundle] pathForResource:@"index" ofType: @"html"];
NSData   * data     = [NSData dataWithContentsOfFile: dataPath];
TFHpple  * document = [[TFHpple alloc] initWithHTMLData:data];

// You can search in the whole document.
TFHppleXPathResult * result  = [document searchWithXPathQuery:@"//a[@class='sponsor']"];

TFHppleElement * element = [result objectAtIndex:0];
[element content];              // Tag's innerHTML
[element tagName];              // "a"
[element attributes];           // NSDictionary of href, class, id, etc.
[element objectForKey:@"href"]; // Easy access to single attribute

// You can search starting from a subnode.
TFHppleElement * subnode = [document peekAtSearchWithXPathQuery:@"//div[@id='footer']"];
result = [subnode searchWithXPathQuery:@"div"]; // direct "div" children of subnode

// The TFHppleXPathResult behaves like an array.
[result count];            // number of matching elements
[result objectAtIndex: 3]; // the 4th element
[result isEmpty];          // BOOL that determines whether the result is empty or not

// You can transform it into an array.
NSArray* arr = [result array];
forin(TFHppleElement* node in arr) {
  NSLog(@"%@", node.tagName);
}

See TFHppleHTMLTest.m and TFHppleXMLTest.m in HppleTests for more examples.

TODO

  • Internal error catching and messages
  • CSS3 selectors in addition to XPath

hpple's People

Contributors

lysannschlegel avatar saleh-hosseinkhani avatar topfunky avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kensla

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.