项目作者: nch3v

项目描述 :
A friendly way to configure a SAX parser
高级语言: JavaScript
项目地址: git://github.com/nch3v/saxo-parser.git
创建时间: 2016-03-25T16:14:00Z
项目社区:https://github.com/nch3v/saxo-parser

开源协议:

下载


SaxoParser

Purpose

SaxoParser is a helper to configure a sax parser using a self descriptive configuration object.

Installation

npm i -S saxo-parser

Usage

  1. var SaxoParser = require('saxo-parser');
  2. var saxo = new SaxoParser({
  3. myTag : {
  4. _open: function(tag) {
  5. tag.data = {};
  6. tag.data.someProp = tag.attributes.someProp;
  7. tag.data.moreProps = [];
  8. },
  9. _close: function(tag) {
  10. // do something with tag.data
  11. console.log(JSON.stringify(tag.data));
  12. },
  13. mySubTag : {
  14. _close: function(tag) {
  15. tag.parent.data.moreProps.push(tag.text);
  16. }
  17. }
  18. }
  19. });
  20. saxo.parseString('<root><myTag someProp="someValue"><mySubTag>Some</mySubTag><mySubTag>Text</mySubTag></myTag></root>');

API

The constructor function takes an object describing what function should be called when parsing each xml element.

The entries of the object can be either a tag element name or a special function _open, _text (matches both text and cdata nodes) or _close. None of the special method is mandatory and should be used depending of the object to be parsed.

The parser can then parse a string with parseString, an inputStream with parseStream or a file with parseFile