forked from alabianca/xml-to-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (30 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const XMLtoJSONstream = require('./lib/xmlToJsonStream');
const traverse = require('./lib/xmlToJson');
const cleanXML = require('./lib/cleanXML');
const validator = require('./lib/validator');
module.exports = function(options) {
const opts = options || {}
const attributeMode = typeof opts.attributeMode === 'undefined' ? true : opts.attributeMode;
const escapeEndLines = typeof options.escapeEndLines === "undefined" ? false : options.escapeEndLines
const createStream = function() {
return new XMLtoJSONstream({attributeMode: attributeMode, escapeEndLines: escapeEndLines});
}
const xmlToJson = function(xml,cb) {
const clean = cleanXML(xml, escapeEndLines);
if(!validator(clean)) {
const err = new Error('Invalid XML. XML is missing closing or opening tag');
return cb(err);
}
let json;
try {
json = traverse(clean,attributeMode);
return cb(null,json);
}catch(e) {
return cb(e);
}
}
return {
createStream : createStream,
xmlToJson : xmlToJson
}
}