Forgiving HTML/XML/RSS Parser in JS for Node
JavaScript
Pull request Compare This branch is 404 commits behind fb55:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
tests
.travis.yml
LICENSE
README.md
package.json

README.md

#htmlparser2 Build Status

A forgiving HTML/XML/RSS parser written in JS for NodeJS. The parser can handle streams (chunked data) and supports custom handlers for writing custom DOMs/output.

##Installing npm install htmlparser2

##How is this different from node-htmlparser? This is a fork of the project above. The main difference is that this is just intended to be used with node (it runs on other platforms using browserify). Besides, the code is much better structured, has less duplications and is remarkably faster than the original.

The parser now provides a callback interface close to sax.js (originally intended for readabilitySAX). I also fixed a couple of bugs & included some pull requests for the original project (eg. RDF feed support).

The support for location data and verbose output was removed a couple of versions ago. It's still available in the verbose branch (if you really need it, for whatever reason that may be).

The DefaultHandler and the RssHandler were renamed to clarify their purpose (to DomHandler and FeedHandler). The old names are still available when requiring htmlparser2, so your code should work as expected.

##Usage

var htmlparser = require("htmlparser2");
var parser = new htmlparser.Parser({
	onopentag: function(name, attribs){
		if(name === "script" && attribs["language"] === "javascript"){
			console.log("JS! Hooray!");
		}
	},
	ontext: function(text){
		console.log("-->", text);
	},
	onclosetag: function(tagname){
		if(tagname === "script"){
			console.log("That's it?!");
		}
	}
});
parser.write("Xyz <script language= javascript>var foo = '<<bar>>';< /  script>");
parser.done();

Output (simplified):

--> Xyz 
JS! Hooray!
--> var foo = '<<bar>>';
That's it?!

Read more about the parser in the wiki.

##Get a DOM The DomHandler (known as DefaultHandler in the original htmlparser module) produces a DOM (document object model) that may be manipulated using the DomUtils helper.

Read more about the DomHandler in the wiki.

##Parsing RSS/RDF/Atom Feeds

new htmlparser.FeedHandler(function (error, feed) {
    ...
});