2013年2月15日 星期五

[Node.js] node-expat, XML parser for node.js

node-expat, if you like to use libxml, this is a simular module for you, in their test.js file, they have many examples for using it. Here is a simple example for it.

var expat = require('node-expat');
var parser = new expat.Parser('UTF-8');


var text = '';

// start point for parsing one tag
parser.addListener('startElement', function(name, attrs) {
   // name : name for this tag
   // attrs : attributes in this tag
});

// Get the value for one tag
parser.addListener('text', function(s) {
   // s : value for this tag
});

// end point for parsing one tag
parser.addListener('endElement', function(name) {
   // name : name for this tag
});

// start to parse
parser.parse(str);


Reference :
node-expat github : https://github.com/astro/node-expat
discussion about html parser : http://stackoverflow.com/questions/7977945/html-parser-on-nodejs

沒有留言:

張貼留言