Skip to content
Browse files

wip

  • Loading branch information...
1 parent 45bd343 commit a10b248a2890484c721f8b5029708e1496f0d585 @c4milo c4milo committed May 15, 2012
Showing with 52 additions and 50 deletions.
  1. +1 −0 .gitignore
  2. +36 −9 lib/xml2json.js
  3. +6 −2 package.json
  4. +9 −39 specs/spec.js
View
1 .gitignore
@@ -1,2 +1,3 @@
node_modules
*.swp
+.settings.xml
View
45 lib/xml2json.js
@@ -9,7 +9,7 @@ var options = {}; //configuration options
function startElement(name, attrs) {
if (! (name in currentObject)) {
currentObject[name] = attrs;
- } else if (! (currentObject[name] instanceof Array)) {
+ } else if (!Array.isArray(currentObject[name])) {
// Put the existing object in an array.
var newArray = [currentObject[name]];
// Add the new object to the array.
@@ -25,7 +25,7 @@ function startElement(name, attrs) {
ancestors.push(currentObject);
// We are now working with this object, so it becomes the current parent.
- if (currentObject[name] instanceof Array) {
+ if (Array.isArray(currentObject[name])) {
// If it is an array, get the last element of the array.
currentObject = currentObject[name][currentObject[name].length - 1];
} else {
@@ -39,16 +39,18 @@ function text(data) {
if (!data.length) {
return;
}
- currentObject['$t'] = (currentObject['$t'] || "") + data;
+ currentObject['$t'] = (currentObject['$t'] || '') + data;
}
function endElement(name) {
- // This should check to make sure that the name we're ending
+ // This should check to make sure that the name we're ending
// matches the name we started on.
var ancestor = ancestors.pop();
if (!options.reversible) {
- if ((Object.keys(currentObject).length == 1) && ('$t' in currentObject)) {
- if (ancestor[name] instanceof Array) {
+ if ((Object.keys(currentObject).length == 1) &&
+ ('$t' in currentObject)) {
+
+ if (Array.isArray(ancestor[name])) {
ancestor[name].push(ancestor[name].pop()['$t']);
} else {
ancestor[name] = currentObject['$t'];
@@ -59,6 +61,29 @@ function endElement(name) {
currentObject = ancestor;
}
+/**
+ * Parses xml to json using node-expat.
+ * @param {String|Buffer} xml The xml to be parsed to json.
+ * @param {Object} _options An object with options provided by the user.
+ * The available options are:
+ * - object: If true, the parser returns a Javascript object instead of
+ * a JSON string.
+ * - reversible: If true, the parser generates a reversible JSON, mainly
+ * characterized by the presence of the property $t.
+ * - sanitize_values: If true, the parser escapes any element value in the xml
+ * that matches one of the following characters:
+ * character | escaped
+ * < &lt;
+ * > &gt;
+ * ( &#40;
+ * ) &#41;
+ * # &#35;
+ * & &amp;
+ * " &quot;
+ * ' &apos;.
+ * @return {String|Object} A String or an Object with the JSON representation
+ * of the XML.
+ **/
module.exports = function(xml, _options) {
var parser = new expat.Parser('UTF-8');
@@ -71,16 +96,18 @@ module.exports = function(xml, _options) {
options = {
object: false,
- reversible: false
+ reversible: false,
+ sanitize_values: false
};
for (var opt in _options) {
options[opt] = _options[opt];
}
if (!parser.parse(xml)) {
- console.log('-->' + xml + '<--');
- throw new Error('There are errors in your xml file: ' + parser.getError());
+ //console.log('-->' + xml + '<--');
+ throw new Error('There are errors in your xml file: ' +
+ parser.getError());
}
if (options.object) {
View
8 package.json
@@ -1,13 +1,17 @@
{ "name" : "xml2json",
- "version": "0.2.4",
+ "version": "0.3.0",
"author": "Andrew Turley",
"email": "aturley@buglabs.net",
"description" : "Converts xml to json and viceverza, using node-expat.",
"repository": "git://github.com/buglabs/node-xml2json.git",
"main": "index",
"contributors":[ {"name": "Camilo Aguilar", "email": "camilo@buglabs.net"}],
+ "devDependencies": {
+ "mocha": "1.0.3",
+ "should": "0.6.3"
+ },
"dependencies": {
- "node-expat": "1.4.1"
+ "node-expat": "1.5.0"
}
}
View
48 specs/spec.js
@@ -1,49 +1,19 @@
var fs = require('fs');
var path = require('path');
-var parser = require('../lib');
-var assert = require('assert');
-var fixturesPath = './fixtures';
+describe('xml2json', function() {
+ it('should convert xml to json');
-fs.readdir(fixturesPath, function(err, files) {
- for (var i in files) {
- var file = files[i];
- var ext = path.extname(file);
+ it('should convert xml to reversible json');
- if (ext == '.xml') {
- var basename = path.basename(file, '.xml');
+ it('should convert xml to a javascript object');
- var data = fs.readFileSync(fixturesPath + '/' + file);
- var result = parser.toJson(data, {reversible: true});
-
- var data2 = fs.readFileSync(fixturesPath + '/' + file);
- result = parser.toJson(data2);
+ it('should escape problematic characters in element values');
+});
- var jsonFile = basename + '.json';
- var expected = fs.readFileSync(fixturesPath + '/' + jsonFile) + '';
+describe('json2xml', function() {
+ it('should return -1 when the value is not present');
- if (expected) {
- expected = expected.trim();
- }
- assert.deepEqual(result, expected, jsonFile + ' and ' + file + ' are different');
- console.log('[xml2json: ' + file + '->' + jsonFile + '] passed!');
- } else if( ext == '.json') {
- var basename = path.basename(file, '.json');
- if (basename.match('reversible')) {
- var data = fs.readFileSync(fixturesPath + '/' + file);
- var result = parser.toXml(data);
-
- var xmlFile = basename.split('-')[0] + '.xml';
- var expected = fs.readFileSync(fixturesPath + '/' + xmlFile) + '';
-
- if (expected) {
- expected = expected.trim();
- }
- //console.log(result + '<---');
- assert.deepEqual(result, expected, xmlFile + ' and ' + file + ' are different');
- console.log('[json2xml: ' + file + '->' + xmlFile + '] passed!');
- }
- }
- }
+ it('should return the correct index when the value is present');
});

0 comments on commit a10b248

Please sign in to comment.
Something went wrong with that request. Please try again.