Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/node2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const convertToJson =function(node, options) {
for (var tag in node.child[tagname]) {
jObj[tagname].push( convertToJson(node.child[tagname][tag], options) );
}
}
else if (options.arrayMode && node.child[tagname]){
jObj[tagname] = [convertToJson(node.child[tagname][0], options)];
} else {
jObj[tagname] = convertToJson(node.child[tagname][0], options);
}
Expand All @@ -35,4 +38,4 @@ const convertToJson =function(node, options) {
return jObj;
};

exports.convertToJson = convertToJson;
exports.convertToJson = convertToJson;
2 changes: 1 addition & 1 deletion src/node2json_str.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ function indentate(options, level) {
return options.indentBy.repeat(level);
}

exports.convertToJsonString = convertToJsonString;
exports.convertToJsonString = convertToJsonString;
31 changes: 31 additions & 0 deletions test/node2json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
parser = require('../src/parser');
assert = require('assert');

const xml = `
<root>
<item id="a">
<sub>1</sub>
<sub>2</sub>
</item>
<item id="b">
<sub>3</sub>
</item>
</root>`

const options = {
ignoreAttributes : false,
ignoreNameSpace : false,
allowBooleanAttributes : true,
parseNodeValue : true,
parseAttributeValue : true,
arrayMode: true
};
const jsonFromXml = parser.parse(xml,options)
const expectedObj = {root:
[ { item:
[ { '@_id': 'a', sub: [ 1, 2 ] },
{ '@_id': 'b', sub: [ 3 ] } ] }
]
}
assert.deepStrictEqual(jsonFromXml, expectedObj)
console.log("\x1b[32m", "done")