0.1.2 • Published 6 years ago
node-xml-lite2 v0.1.2
node-xml-lite2
A stripped down fork of https://github.com/hgourvest/node-xml-lite
- This is a pure javascript XML SAX parser for Node.js.
 - The specificity of this xml parser is that it can parse a document from a Buffer.
 It relies on iconv-lite to decode the text according to the code page of the document.
Differences from node-xml-lite
- Removed all 
SAXParse*API variants until I have time to rewrite them into aparseStreamAPI - Exported the raw 
XmlParserclass to allow for custom, lightweight parsing jobs (somewhat making up for the removal of theSAXParse*APIs) - Added TypeScript definition file
 - Added an 
offsetproperty to the parser to complementline+col 
Installation
npm install node-xml-lite2Sample usage
Parse a file
var xml = require('node-xml-lite2');
xml.parseFile('~/test.xml', function(err, root) {
  // ...
});Parse a file synchronously
var xml = require('node-xml-lite2');
var root = xml.parseFileSync('~/test.xml');Parse a string
var xml = require('node-xml-lite2');
xml.parseString('<xml>hello</xml>');Parse a buffer
var xml = require('node-xml-lite2');
xml.parseBuffer(Buffer.from('<xml>hello</xml>', 'utf8'));Advanced usage
Custom parsing
var xml = require('node-xml-lite2');
var parser = new xml.XmlParser();
var buffer = fs.readFileSync('~/large-file.xml');
parser.parseBuffer(buffer, buffer.length, function(state, name, value) {
  switch (state) {
    case xml.xtOpen:
      console.log('opening:', name);
      break;
    case xml.xtClose:
      console.log('closing:', name);
      break;
    case xml.xtAttribute:
      console.log('attribute:', name + '=' + value);
      break;
    case xml.xtCData:
      console.log('CDATA:', name);
      break;
    case xml.xtComment:
      console.log('comment:', name);
      break;
  }
  return true;
});