Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #24 from fengmk2/issue23-support-spacetext
Browse files Browse the repository at this point in the history
fixed #23 support xml space text using `options.space`
  • Loading branch information
c4milo committed Aug 13, 2012
2 parents 21d8385 + 1f29dcd commit 8538475
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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 keep xml space text then you should use `options.space`, `parser.toJson(xml, {space: true})`;

## License
(The MIT License)
Expand Down
17 changes: 13 additions & 4 deletions lib/xml2json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ var fs = require('fs');
var obj = {};
var currentObject = {};
var ancestors = [];
var currentElementName = null;

var options = {}; //configuration options
function startElement(name, attrs) {
currentElementName = name;
if(options.coerce) {
// Looping here in stead of making coerce generic as object walk is unnecessary
for (var key in attrs) {
Expand Down Expand Up @@ -44,14 +46,19 @@ function startElement(name, attrs) {
}

function text(data) {
data = data.trim();
if (!data.length) {
return;
if (!options.space) {
data = data.trim();
if (!data.length) {
return;
}
}
currentObject['$t'] = coerce((currentObject['$t'] || "") + data);
}

function endElement(name) {
if (options.space && currentElementName !== name) {
delete currentObject['$t'];
}
// This should check to make sure that the name we're ending
// matches the name we started on.
var ancestor = ancestors.pop();
Expand Down Expand Up @@ -96,10 +103,12 @@ module.exports = function(xml, _options) {

obj = currentObject = {};
ancestors = [];
currentElementName = null;

options = {
object: false,
reversible: false
reversible: false,
space: false, // keep space or not
};

for (var opt in _options) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/coerce.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"itemRecord":{"value":[{"longValue":"12345"},{"stringValue":{"number":"false","$t":"this is a string value"}},{"moneyValue":{"number":"true","currencyId":"USD","$t":"104.95"}},{"moneyValue":{"currencyId":"USD","$t":"104.95"}},{"longValue":"0","bool":{"id":"0","$t":"true"}},{"longValue":"0"},{"dateValue":"2012-02-16T17:03:33.000-07:00"},{"stringValue":"SmDZ8RlMIjDvlEW3KUibzj2Q"}]}}
1 change: 1 addition & 0 deletions test/fixtures/large.json

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions test/fixtures/reorder.json
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
{
"parent":
{
"child": { "child_property": "foo" },
"parent_property": "bar"
}
}
{"parent":{"parent_property":"bar","child":{"child_property":"foo"}}}
1 change: 1 addition & 0 deletions test/fixtures/spacetext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"doc":{"Column":[{"Name":"shit","Value":{"type":"STRING","$t":" abc\nasdf\na "}},{"Name":"foo","Value":{"type":"STRING"}},{"Name":"foo2","Value":{"type":"STRING"}},{"Name":"bar","Value":{"type":"STRING","$t":" "}},{"PK":"true","Name":"uid","Value":{"type":"STRING","$t":"god"}}]}}
14 changes: 14 additions & 0 deletions test/fixtures/spacetext.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<doc>
<Column>
<Name>shit</Name>
<Value type="STRING"> abc
asdf
a </Value>

</Column>
<Column><Name>foo</Name><Value type="STRING"></Value></Column>
<Column><Name>foo2</Name><Value type="STRING" /></Column>
<Column><Name>bar</Name><Value type="STRING"> </Value></Column>
<Column PK="true"><Name>uid</Name><Value type="STRING">god</Value></Column>
</doc>

25 changes: 25 additions & 0 deletions test/test-space.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var fs = require('fs');
var path = require('path');
var parser = require('../lib');
var assert = require('assert');

var xml = fs.readFileSync(__dirname + '/fixtures/spacetext.xml');
var json = parser.toJson(xml, {object: true, space: true});
console.log('xml => json: \n%j', json);

console.log('---------------------\njson => xml: \n%j\n',
parser.toXml(fs.readFileSync(__dirname + '/fixtures/spacetext.json')));
function eql(a, b) {
for (var k in a) {
assert.deepEqual(a[k], b[k], JSON.stringify(a) + ' should equal ' + JSON.stringify(b));
}
}

assert.deepEqual(json.doc.Column.length, 5, 'should have 5 Columns');
eql(json.doc.Column[0], {Name: 'shit', Value: {type: 'STRING', $t: ' abc\nasdf\na '}});
eql(json.doc.Column[1], {Name: 'foo', Value: {type: 'STRING'}});
eql(json.doc.Column[2], {Name: 'foo2', Value: {type: 'STRING'}});
eql(json.doc.Column[3], {Name: 'bar', Value: {type: 'STRING', $t: ' '}});
eql(json.doc.Column[4], {PK: 'true', Name: 'uid', Value: {type: 'STRING', $t: 'god'}});

console.log('xml2json options.space passed!');
6 changes: 5 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ fs.readdir(fixturesPath, function(err, files) {
var result = parser.toJson(data, {reversible: true});

var data2 = fs.readFileSync(fixturesPath + '/' + file);
result = parser.toJson(data2);
if (file.indexOf('spacetext') >= 0) {
result = parser.toJson(data2, {space: true});
} else {
result = parser.toJson(data2);
}

var jsonFile = basename + '.json';
var expected = fs.readFileSync(fixturesPath + '/' + jsonFile) + '';
Expand Down

0 comments on commit 8538475

Please sign in to comment.