SAX:js解析XML插件sax,能获得行号信息挺方便

官网:https://www.npmjs.com/package/sax

github:https://github.com/isaacs/sax-js

此种方式可以非常方便的获取行号信息,很容易对XML定位出错,对于前端XML编辑器,对XML数据进行校验,定位到出错行非常方便。

 

使用方法:

Usage
var sax = require("./lib/sax"),
  strict = true, // set to false for html-mode 
  parser = sax.parser(strict);
 
parser.onerror = function (e) {
  // an error happened. 
};
parser.ontext = function (t) {
  // got some text.  t is the string of text. 
};
parser.onopentag = function (node) {
  // opened a tag.  node has "name" and "attributes" 
};
parser.onattribute = function (attr) {
  // an attribute.  attr has "name" and "value" 
};
parser.onend = function () {
  // parser stream is done, and ready to have more stuff written to it. 
};
 
parser.write('<xml>Hello, <who name="world">world</who>!</xml>').close();
 
// stream usage 
// takes the same options as the parser 
var saxStream = require("sax").createStream(strict, options)
saxStream.on("error", function (e) {
  // unhandled errors will throw, since this is a proper node 
  // event emitter. 
  console.error("error!", e)
  // clear the error 
  this._parser.error = null
  this._parser.resume()
})
saxStream.on("opentag", function (node) {
  // same object as above 
})
// pipe is supported, and it's readable/writable 
// same chunks coming in also go out. 
fs.createReadStream("file.xml")
  .pipe(saxStream)
  .pipe(fs.createWriteStream("file-copy.xml"))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值