Skip to content

Instantly share code, notes, and snippets.

@vflash
Created July 22, 2013 12:41
Show Gist options
  • Save vflash/6053548 to your computer and use it in GitHub Desktop.
Save vflash/6053548 to your computer and use it in GitHub Desktop.
var EasySax = require("easysax");
var FS = require('fs');
var xmlFile = './xxx.xml';
var parser = new EasySax();
/* // если нужно пространство имен
parser.ns('rss', {
'http://www.w3.org/2005/Atom': 'atom',
'http://www.w3.org/1999/xhtml': 'xhtml'
});
*/
parser.on('startNode', function(elem, attr, unEntities, tagend, getStringNode) {
// elem - text, node name
// attr - fucntion
// unEntities() - fucntion
// tagend - boolean
// getStringNode() - fucntion
console.log('startNode - ' + elem);
console.log(attr() ); // если false то ошибка синтаксиса. true - атрибутов нет, но разбор удачен
if (tagend) {
console.log('endNode');
};
});
parser.on('endNode', function(elem, unEntities, tagstart, getStringNode) {
if (!tagstart) {
console.log('endNode - ' + elem);
};
});
parser.on('textNode', function(text, unEntities) {
console.log('text - ' + unEntities(text) );
});
parser.on('cdata', function(text) {
console.log('cdata - ' + unEntities(text) );
});
parser.on('error', function(msg) {
console.log('error - ' + msg );
});
FS.readFile(xmlFile, function (err, data) {
if (err) {
console.log('file:// not load')
return;
};
var xml = data.toString();
parser.parse(xml)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment