Skip to content

Forgiving HTML/XML/RSS parser for node in pure JavaScript

License

Notifications You must be signed in to change notification settings

myndzi/node-htmlparser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

#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

##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 can be manipulated using the DomUtils helper.

The DomHandler, while still bundled with this module, was recently moved to it's own module. Have a look at it for further information.

##Parsing RSS/RDF/Atom Feeds

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

##Performance Using a slightly modified version of node-expats bench.js, I received the following results (on a MacBook (late 2010)):

The test may be found in tests/bench.js.

##How is this different from node-htmlparser? This is a fork of the project above. The main difference is that this is intended to be used only 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 targeted at 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.

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.

About

Forgiving HTML/XML/RSS parser for node in pure JavaScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%