New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

xml2js-expat

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml2js-expat - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

58

lib/xml2js.js

@@ -11,11 +11,39 @@ var expat = require('node-expat'),

* Expat is a SAX style XML parser that is really fast.
*
* Emits `end` when parsing ends.
*
* @param {String|Function} [encoding]
* Expected character encoding of the XML to be parsed.
* Built in encodings are:
* - UTF-8
* - UTF-16
* - ISO-8859-1
* - US-ASCII
*
* If the encoding is a function, it will be treated as `callback`.
*
* @param {Function} [callback]
* Optional function to be called when the parsing ends.
*/
var Parser = function (encoding) {
var Parser = function (encoding, callback) {
var that = this,
stack = [];
this.saxParser = new expat.Parser(encoding || 'UTF-8'); // make the sax parser
this.EXPLICIT_CHARKEY = false; // always use the '#' key, even if there are no subkeys
stack = [],
defaultEncoding = 'UTF-8';
if (typeof encoding === 'function') {
callback = encoding;
encoding = defaultEncoding;
}
// Make the sax parser.
this.saxParser = new expat.Parser(encoding || defaultEncoding);
// Always use the '#' key, even if there are no subkeys.
this.EXPLICIT_CHARKEY = false;
this.resultObject = null;
this.saxParser.addListener('startElement', function (name, attributes) {
if (callback) {
this.on('end', callback);
}
// New element arrived handle it.
this.saxParser.on('startElement', function (name, attributes) {
var obj = {}, keys;

@@ -34,7 +62,8 @@

}
obj['#name'] = name; // need a place to store the node name
obj['#name'] = name; // Need a place to store the node name.
stack.push(obj);
});
this.saxParser.addListener('endElement', function (name) {
// Element ended, clean up.
this.saxParser.on('endElement', function (name) {
var obj = stack.pop(),

@@ -47,12 +76,12 @@ nodeName = obj['#name'],

// remove the '#' key altogether if it's blank
// Remove the '#' key altogether if it's blank.
if (obj['#'].match(/^\s*$/)) {
delete obj['#'];
} else {
// turn 2 or more spaces into one space
// Turn 2 or more spaces into one space.
obj['#'] = obj['#'].replace(/\s{2,}/g, " ").trim();
// also do away with '#' key altogether, if there's no subkeys
// unless EXPLICIT_CHARKEY is set
if (Object.keys(obj).length === 1 && '#' in obj && !(that.EXPLICIT_CHARKEY)) {
// Also do away with '#' key altogether, if there's no subkeys
// unless EXPLICIT_CHARKEY is set.
if (Object.keys(obj).length === 1 && '#' in obj && !that.EXPLICIT_CHARKEY) {
obj = obj['#'];

@@ -62,3 +91,3 @@ }

// set up the parent element relationship
// Set up the parent element relationship.
if (stack.length > 0) {

@@ -78,6 +107,7 @@ if (typeof s[nodeName] === 'undefined') {

that.resultObject = obj;
that.emit("end", that.resultObject);
that.emit("end", that.resultObject, that.getError());
}
});
// New text element.
this.saxParser.addListener('text', function (text) {

@@ -84,0 +114,0 @@ var s = stack[stack.length - 1];

{
"name": "xml2js-expat",
"version": "0.1.3",
"version": "0.2.0",
"description": "Simple XML to JavaScript object converter that uses Expat, a fast XML parser.",

@@ -5,0 +5,0 @@ "homepage" : "https://github.com/Poetro/node-xml2js-expat",

@@ -17,4 +17,3 @@ node-xml2js-expat

var parser = new xml2js.Parser();
parser.addListener('end', function(result, error) {
var parser = new xml2js.Parser(function(result, error) {
if (!error) {

@@ -36,1 +35,23 @@ console.log(sys.inspect(result));

});
The Parser object has an event, that can be suscribed to with
parser.on('end', function (result, error) {});
// or
parser.addListener('end', function (result, error) {});
The Parser object supports the following encodings, that can be specified as the first parameter, in which case the callback should be the second. (Each argument is optional.)
- `UTF-8`
- `UTF-16`
- `ISO-8859-1`
- `US-ASCII`
For example:
var parser = new xml2js.Parser('UTF-8', function(result, error) {});
or
var parser = new xml2js.Parser('UTF-8');
parser.on('end', function (result, error) {});

@@ -7,7 +7,3 @@ var xml2js = require('../lib/xml2js'),

'test default parse' : function(assert) {
var x2js = new xml2js.Parser();
assert.notStrictEqual(x2js, undefined);
x2js.addListener('end', function(r) {
var x2js = new xml2js.Parser(function(r) {
console.log('Result object: ' + sys.inspect(r, false, 10));

@@ -30,2 +26,4 @@ assert.equal(r['chartest']['@']['desc'], "Test for CHARs");

assert.notStrictEqual(x2js, undefined);
fs.readFile(__dirname + '/fixtures/sample.xml', function(err, data) {

@@ -37,8 +35,3 @@ assert.strictEqual(err, null);

'test parse EXPLICIT_CHARKEY' : function(assert) {
var x2js = new xml2js.Parser();
assert.notStrictEqual(x2js, undefined);
x2js.EXPLICIT_CHARKEY = true;
x2js.addListener('end', function(r) {
var x2js = new xml2js.Parser(function(r) {
console.log('Result object: ' + sys.inspect(r, false, 10));

@@ -61,2 +54,5 @@ assert.equal(r['chartest']['@']['desc'], "Test for CHARs");

assert.notStrictEqual(x2js, undefined);
x2js.EXPLICIT_CHARKEY = true;
fs.readFile(__dirname + '/fixtures/sample.xml', function(err, data) {

@@ -63,0 +59,0 @@ assert.strictEqual(err, null);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc