百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

node-xml

> 编程语言
开源

一个用 JavaScript 编写的 Node.js 的 XML 解析器。

197 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个用 JavaScript 编写的 Node.js 的 XML 解析器。

node-xml

(C) Rob Righter (@robrighter) 2009 - 2010, Licensed under the MIT-LICENSE Contributions from David Joham

node-xml is an xml parser for node.js written in javascript.

Install

bash
	npm install node-xml

API

SaxParser

Node-xml provides a SAX2 parser interface that can take a string, file. The parser can take characters from the document in chunks. To send chunks of the document to the parser use 'parseString(xml)'

#SAX Parser#

##new xml.SaxParser()## * Instantiate a new SaxParser * returns: a SaxParser object

##new xml.SaxParser(callback)## * Instantiate a new SaxParser * returns: a SaxParser object * Arguments *callback - a function that accepts the new sax parser as an argument

#Parse#

##parser.parseString(string)##

Parse an in memory string

  • return: boolean. true if no errors, false otherwise
  • Arguments
    • string - a string representing the document to parse

##parser.parseFile(filename)##

Parse a file

  • return: boolean. true if no errors, false otherwise
  • Arguments
    • filename - a string representing the file to be parsed

##parser.pause()## pauses parsing of the document

##parser.resume()## resumes parsing of the document

#Callbacks#

##parser.onStartDocument(function() {})##

Called at the start of a document

##parse.onEndDocument(function() {})##

Called at the end of the document parse

##parser.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {})##

Called on an open element tag

  • Arguments
    • elem - a string representing the element name
    • attrs - an array of arrays: [[key, value], [key, value]]
    • prefix - a string representing the namespace prefix of the element
    • uri - the namespace URI of the element
    • namespaces - an array of arrays: [[prefix, uri], [prefix, uri]]

##parser.onEndElementNS(function(elem, prefix, uri) {})##

Called at the close of an element

  • Arguments
    • elem - a string representing the element name
    • prefix - a string representing the namespace prefix of the element
    • uri - the namespace URI of the element

##parser.onCharacters(function(chars) {})##

Called when a set of content characters is encountered

  • Arguments
    • chars - a string of characters

##parser.onCdata(function(cdata) {})##

Called when a CDATA is encountered

  • Arguments
    • cdata - a string representing the CDATA

##parser.onComment(function(msg) {})##

Called when a comment is encountered

  • Arguments
    • msg - a string representing the comment

##parser.onWarning(function(msg) {})##

Called when a warning is encountered

  • Arguments
    • msg - a string representing the warning message

##parser.onError(function(msg) {})##

Called when an error is encountered

  • Arguments
    • msg - a string representing the error message

EXAMPLE USAGE

var util = require('util');
var xml = require("./lib/node-xml");

var parser = new xml.SaxParser(function(cb) {
  cb.onStartDocument(function() {
	
  });
  cb.onEndDocument(function() {
	
  });
  cb.onStartElementNS(function(elem, attrs, prefix, uri, namespaces) {
      util.log("=> Started: " + elem + " uri="+uri +" (Attributes: " + JSON.stringify(attrs) + " )");
  });
  cb.onEndElementNS(function(elem, prefix, uri) {
      util.log("'+chars+"");
  });
  cb.onCdata(function(cdata) {
      util.log(''+cdata+"");
  });
  cb.onComment(function(msg) {
      util.log(''+msg+"");
  });
  cb.onWarning(function(msg) {
      util.log(''+msg+"");
  });
  cb.onError(function(msg) {
      util.log(''+JSON.stringify(msg)+"");
  });
});


//example read from chunks
parser.parseString("");
parser.parseString("");
parser.parseString("and lots");
parser.parseString("and lots of text&am");
parser.parseString("p;some more.");
parser.parseString("");
parser.parseString("");

//example read from file
parser.parseFile("sample.xml");

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言