How to use the parser function from sax

Find comprehensive JavaScript sax.parser code examples handpicked from public code repositorys.

sax.parser is a SAX-based XML parser for Node.js that provides a simple way of parsing XML documents.

104
105
106
107
108
109
110
111
112
113
// Setup parser {{{
var parser;
if (input instanceof stream.Stream) {
	parser = sax.createStream(true, {});
} else if (typeof input == 'string' || Buffer.isBuffer(input)) {
	parser = sax.parser(true);
	parser.on = function(event, cb) { // Quick binder to simulate on() behaviour
		parser['on' + event] = cb;
		return parser;
	};
fork icon1
star icon1
watch icon0

9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
  error.note = error; //console.error(error);
}


module.exports = function (xml, userOptions) {


  var parser = pureJsParser ? sax.parser(true, {}) : parser = new expat.Parser('UTF-8');
  var result = {};
  currentElement = result;


  options = validateOptions(userOptions);
fork icon0
star icon7
watch icon2

How does sax.parser work?

sax.parser is a function in the sax library that creates a new parser object that can be used to parse an XML document, emit events for each XML node, and call corresponding callbacks when those events occur. It is a low-level parser that can be used to efficiently parse large XML documents. The parser function takes an optional configuration object as its argument, which can be used to specify various options for the parser, such as the case sensitivity of tags and attributes, and whether or not to include namespaces in the output.

22
23
24
25
26
27
28
29
30
31

	app.onerror = () => {};
`,
outdent`
	const sax = require('sax');
	const parser = sax.parser();

	parser.onerror = () => {};
`,
outdent`
fork icon0
star icon0
watch icon1

+ 5 other calls in file

413
414
415
416
417
418
419
420
421
422
 * @param {Object} options
 * @returns {Promise<Array.<Object>>}
 */
const getDashManifest = (url, options) => new Promise((resolve, reject) => {
  let formats = {};
  const parser = sax.parser(false);
  parser.onerror = reject;
  let adaptationSet;
  parser.onopentag = node => {
    if (node.name === 'ADAPTATIONSET') {
fork icon0
star icon0
watch icon1

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
const sax = require("sax");

const parser = sax.parser(true); // 'true' enables strict mode

parser.onopentag = (tag) => {
  console.log("Open tag:", tag.name);
};

parser.onclosetag = (tagName) => {
  console.log("Close tag:", tagName);
};

parser.write(" John Doe 42 ").close();

In this example, we create a sax.parser object by calling sax.parser(true), which enables strict mode. We then set up event listeners for the onopentag and onclosetag events. Finally, we pass the XML document to be parsed to the parser by calling write() and then close(). As the parser encounters opening and closing tags in the document, it will trigger the appropriate event listeners, in this case logging the tag names to the console.

4
5
6
7
8
9
10
11
12
13
14
15
16


var Transform = require('stream').Transform;
var sax = require('sax');


function parseXML(str, cb) {
  var obj, current, parser = sax.parser(true, { trim: true, lowercase: true })
  parser.onerror = parser.onend = done;


  function done(err) {
    parser.onerror = parser.onend = function() { }
fork icon0
star icon0
watch icon1

+ 2 other calls in file

62
63
64
65
66
67
68
69
70
71
72
 * @param {String=} fileName
 * @return {Array.<BPMNProcessDefinition>}
 */
exports.parse = function(bpmnXML, errorQueue, mainProcessName, fileName) {


    var parser = sax.parser(true, {"xmlns": true});
    var topLevelDefinitions = [];
    var processStack = [];
    /** @type {BPMNProcessDefinition} */
    var subProcessDefinition = null;
fork icon0
star icon0
watch icon0