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

Commit 6facb9a

Browse files
committed
workaround for issue #1
1 parent 200043b commit 6facb9a

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

lib/index.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
var expat = require('node-expat');
22
var fs = require('fs');
3-
var parser = new expat.Parser('UTF-8');
43

54
// This object will hold the final result.
6-
var obj = {};
7-
var currentObject = obj;
5+
var obj = currentObject = {};
86
var ancestors = [];
97

108
var options = {}; //configuration options
9+
1110
function startElement(name, attrs) {
1211
if (! (name in currentObject)) {
1312
currentObject[name] = attrs;
@@ -66,26 +65,35 @@ function endElement(name) {
6665
currentObject = ancestor;
6766
}
6867

69-
parser.on('startElement', startElement);
70-
parser.on('text', text);
71-
parser.on('endElement', endElement);
7268

7369
module.exports.toJson = function(xml, _options) {
74-
options = null;
70+
var parser = new expat.Parser('UTF-8');
71+
72+
parser.on('startElement', startElement);
73+
parser.on('text', text);
74+
parser.on('endElement', endElement);
75+
76+
obj = currentObject = {};
77+
ancestors = [];
78+
7579
options = {
7680
object: false,
7781
reversible: false
78-
}
82+
};
7983

8084
for (var opt in _options) {
8185
options[opt] = _options[opt];
8286
}
8387

84-
if (parser.parse(xml)) {
85-
if (options.object) {
86-
return obj;
87-
}
88-
return JSON.stringify(obj);
88+
if (!parser.parse(xml)) {
89+
console.log('-->'+ xml + '<--');
90+
throw new Error('There are errors in your xml file: ' + parser.getError());
91+
}
92+
93+
if (options.object) {
94+
return obj;
8995
}
96+
97+
return JSON.stringify(obj);
9098
};
9199

test/test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ fs.readdir(fixturesPath, function(err, files) {
1515

1616
var data = fs.readFileSync(fixturesPath + '/' + file);
1717
var result = parser.toJson(data);
18+
var data2 = fs.readFileSync(fixturesPath + '/' + file);
19+
result = parser.toJson(data2);
1820

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

0 commit comments

Comments
 (0)