Skip to content

add `attributes` and `textNodeName` options to xml parser #15

Closed
wants to merge 2 commits into from
View
3 README.md
@@ -21,7 +21,8 @@ console.log(json);
```
* if you want to get the Javascript object then you might want to invoke parser.toJson(xml, {object: true});
* if you want a reversible json to xml then you should use parser.toJson(xml, {reversible: true});
-
+* if you want to override the default "$t" key name for a node's text value, specify the textNodeKey option. e.g., `parser.toJson(xml, {textNodeKey: "text"})`
+* if you want to prevent xml node attributes from appearing in the json, specify attributes: false. e.g., `parser.toJson(xml, {attributes: false})`. The default behavior is attributes: true.
## License
Copyright 2011 BugLabs Inc. All rights reserved.
View
15 lib/xml2json.js
@@ -7,6 +7,9 @@ var ancestors = [];
var options = {}; //configuration options
function startElement(name, attrs) {
+ // if options.attributes is false, throw away attributes
+ if (!options.attributes) attrs = {};
+
if (! (name in currentObject)) {
currentObject[name] = attrs;
} else if (! (currentObject[name] instanceof Array)) {
@@ -39,19 +42,19 @@ function text(data) {
if (!data.length) {
return;
}
- currentObject['$t'] = (currentObject['$t'|| "") + data;
+ currentObject[options.textNodeKey] = (currentObject[options.textNodeKey|| "") + data;
}
function endElement(name) {
// 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 ((Object.keys(currentObject).length == 1) && (options.textNodeKey in currentObject)) {
if (ancestor[name] instanceof Array) {
- ancestor[name].push(ancestor[name].pop()['$t']);
+ ancestor[name].push(ancestor[name].pop()[options.textNodeKey]);
} else {
- ancestor[name] = currentObject['$t'];
+ ancestor[name] = currentObject[options.textNodeKey];
}
}
}
@@ -71,7 +74,9 @@ module.exports = function(xml, _options) {
options = {
object: false,
- reversible: false
+ reversible: false,
+ textNodeKey: '$t',
+ attributes: true
};
for (var opt in _options) {
View
18 test/test.js
@@ -47,3 +47,21 @@ fs.readdir(fixturesPath, function(err, files) {
}
});
+// test options.textNodeKey custom value
+var xml = '<a href="http://example.com">Example Site</a>';
+var actual = parser.toJson(xml, {object: true, textNodeKey: 'hi'});
+var json = '{"a":{"href":"http://example.com","hi":"Example Site"}}';
+assert.deepEqual(actual, JSON.parse(json));
+
+// test options.textNodeKey default value
+var actual = parser.toJson(xml, {object: true});
+var json = json.replace('"hi":', '"$t":');
+assert.deepEqual(actual, JSON.parse(json));
+
+console.log('[xml2json options.textNodeKey] passed');
+
+// test options.attributes = false
+var actual = parser.toJson(xml, {object: true, attributes: false});
+var json = '{"a": "Example Site"}'
+assert.deepEqual(actual, JSON.parse(json));
+console.log('[xml2json options.attributes] passed');
Something went wrong with that request. Please try again.