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

Commit 1df7d4d

Browse files
committed
fixed #23 support xml space text using options.space
1 parent 21d8385 commit 1df7d4d

9 files changed

Lines changed: 60 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ console.log(json);
2121
```
2222
* if you want to get the Javascript object then you might want to invoke parser.toJson(xml, {object: true});
2323
* if you want a reversible json to xml then you should use parser.toJson(xml, {reversible: true});
24-
24+
* if you want to keep xml space text then you should use `options.space`, `parser.toJson(xml, {space: true})`;
2525

2626
## License
2727
(The MIT License)

lib/xml2json.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ var fs = require('fs');
55
var obj = {};
66
var currentObject = {};
77
var ancestors = [];
8+
var currentElementName = null;
89

910
var options = {}; //configuration options
1011
function startElement(name, attrs) {
12+
currentElementName = name;
1113
if(options.coerce) {
1214
// Looping here in stead of making coerce generic as object walk is unnecessary
1315
for (var key in attrs) {
@@ -44,14 +46,19 @@ function startElement(name, attrs) {
4446
}
4547

4648
function text(data) {
47-
data = data.trim();
48-
if (!data.length) {
49-
return;
49+
if (!options.space) {
50+
data = data.trim();
51+
if (!data.length) {
52+
return;
53+
}
5054
}
5155
currentObject['$t'] = coerce((currentObject['$t'] || "") + data);
5256
}
5357

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

97104
obj = currentObject = {};
98105
ancestors = [];
106+
currentElementName = null;
99107

100108
options = {
101109
object: false,
102-
reversible: false
110+
reversible: false,
111+
space: false, // keep space or not
103112
};
104113

105114
for (var opt in _options) {

test/fixtures/coerce.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}]}}

test/fixtures/large.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

test/fixtures/reorder.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
{
2-
"parent":
3-
{
4-
"child": { "child_property": "foo" },
5-
"parent_property": "bar"
6-
}
7-
}
1+
{"parent":{"parent_property":"bar","child":{"child_property":"foo"}}}

test/fixtures/spacetext.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}}]}}

test/fixtures/spacetext.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<doc>
2+
<Column>
3+
<Name>shit</Name>
4+
<Value type="STRING"> abc
5+
asdf
6+
a </Value>
7+
8+
</Column>
9+
<Column><Name>foo</Name><Value type="STRING"></Value></Column>
10+
<Column><Name>foo2</Name><Value type="STRING" /></Column>
11+
<Column><Name>bar</Name><Value type="STRING"> </Value></Column>
12+
<Column PK="true"><Name>uid</Name><Value type="STRING">god</Value></Column>
13+
</doc>
14+

test/test-space.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var parser = require('../lib');
4+
var assert = require('assert');
5+
6+
var xml = fs.readFileSync(__dirname + '/fixtures/spacetext.xml');
7+
var json = parser.toJson(xml, {object: true, space: true});
8+
console.log('%j', json);
9+
10+
function eql(a, b) {
11+
for (var k in a) {
12+
assert.deepEqual(a[k], b[k], JSON.stringify(a) + ' should equal ' + JSON.stringify(b));
13+
}
14+
}
15+
16+
assert.deepEqual(json.doc.Column.length, 5, 'should have 5 Columns');
17+
eql(json.doc.Column[0], {Name: 'shit', Value: {type: 'STRING', $t: ' abc\nasdf\na '}});
18+
eql(json.doc.Column[1], {Name: 'foo', Value: {type: 'STRING'}});
19+
eql(json.doc.Column[2], {Name: 'foo2', Value: {type: 'STRING'}});
20+
eql(json.doc.Column[3], {Name: 'bar', Value: {type: 'STRING', $t: ' '}});
21+
eql(json.doc.Column[4], {PK: 'true', Name: 'uid', Value: {type: 'STRING', $t: 'god'}});
22+
23+
console.log('xml2json options.space passed!');

test/test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ fs.readdir(fixturesPath, function(err, files) {
1717
var result = parser.toJson(data, {reversible: true});
1818

1919
var data2 = fs.readFileSync(fixturesPath + '/' + file);
20-
result = parser.toJson(data2);
20+
if (file.indexOf('spacetext') >= 0) {
21+
result = parser.toJson(data2, {space: true});
22+
} else {
23+
result = parser.toJson(data2);
24+
}
2125

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

0 commit comments

Comments
 (0)