Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

libxml-to-js

Package Overview
Dependencies
Maintainers
0
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

libxml-to-js - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

.project

73

lib/libxml-to-js.js

@@ -67,3 +67,3 @@ var libxmljs = require('libxmljs');

exports.stringParser = function (xml, callback) {
module.exports = function (xml, callback) {
try {

@@ -76,72 +76,1 @@ callback(undefined, libxml2js(libxmljs.parseXmlString(xml)));

exports.saxParser = function (xml, callback) {
var parser = new libxmljs.SaxParser(function (cb) {
var stack = [], end = false;
cb.onStartElementNS(function (elem, attrs) {
var obj = {};
obj['#'] = '';
obj['#name'] = elem;
if (attrs.length) {
obj['@'] = {};
var attrName, attrValue;
for (var i = 0; i < attrs.length; i++) {
attrName = attrs[i][0];
attrValue = attrs[i][3];
obj['@'][attrName] = attrValue;
}
}
stack.push(obj);
});
cb.onEndElementNS(function () {
var obj = stack.pop();
var nodeName = obj['#name'];
delete(obj['#name']);
var s = stack[stack.length - 1];
obj['#'] = obj['#'].replace(/^\s*/, '').replace(/\s*$/, '');
if (obj['#'].match(/^\s*$/)) {
delete(obj['#']);
} else {
if (Object.keys(obj).length === 1 && '#' in obj) {
obj = obj['#'];
}
}
if (stack.length > 0) {
var old;
if ( ! (nodeName in s)) {
return s[nodeName] = obj;
} else if (s[nodeName] instanceof Array) {
return s[nodeName].push(obj);
} else {
old = s[nodeName];
s[nodeName] = [old];
return s[nodeName].push(obj);
}
} else {
callback(undefined, obj);
end = true;
}
});
cb.onCharacters(function (chars) {
var s = stack[stack.length - 1];
if (s) {
return s['#'] += chars;
}
});
cb.onError(function (error) {
if ( ! end) {
callback(error, {});
}
});
});
parser.parseString(xml);
};
{
"name": "libxml-to-js",
"version": "0.1.0",
"version": "0.2.0",
"main": "./lib/libxml-to-js.js",

@@ -5,0 +5,0 @@ "description": "XML to JavaScript object parser based on libxmljs",

## About
This is a XML to JavaScript object parser. It uses the [libxmljs](https://github.com/polotek/libxmljs) module for the actual XML parsing. It aims to be an easy [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js) replacement. I used xml2js for my own needs, but the error reporting of the underlying SAX parser is quite broken.
This is a XML to JavaScript object parser. It uses the [libxmljs](https://github.com/polotek/libxmljs) module for the actual XML parsing. It aims to be an easy [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js) replacement, but it doesn't follow the xml2js API. I used xml2js for my own needs, but the error reporting of the underlying SAX parser is quite broken. This is how libxml-to-js saw the day light.
It has a couple of implemented parsing modes. This is a side effect of the fact that at first I implemented the SAX parser, thereafter the string parser of libxmljs. The first has better error reporting while the last works asynchronously.
libxml-to-js uses the string parser method of libxmljs. Basically a modified version of the algorithm from [here](http://mscdex.net/code-snippets/) in order to fit the formal specifications of xml2js output.

@@ -15,6 +15,4 @@ ## Installation

### The string parser
<pre>
var parser = require('libxml-to-js').stringParser;
var parser = require('libxml-to-js');
var xml = 'xml string';

@@ -31,19 +29,5 @@

### The SAX parser
## Known issues
<pre>
var parser = require('libxml-to-js').saxParser;
var xml = 'xml string';
parser(xml, function (error, result) {
if (error) {
console.error(error);
} else {
console.log(result);
}
});
</pre>
## Notes
The SAX parser usage is identical to the string parser usage. The only actual differece might be an error at the end of the XML string while the rest of the string being valid XML. In this case, the string parser returns the error argument while the SAX parser doesn't return the error, but succesfully return the XML contents. However, the string parser might be buggier if I messed up the recursion. Currently I didn't find anything that breaks it, but your mileage may vary.
* The namespace attribute isn't translated to the JavaScript object as the libxmljs treats it different from the [sax-js](https://github.com/isaacs/sax-js) parser. The sax-js simply places it as a simple attribute.
* The behavior for nodes that use namespaces is untested vs. the output of xml2js.
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