|
1 | | -var expat = require('node-expat'); |
2 | | -var fs = require('fs'); |
3 | | - |
4 | | -// This object will hold the final result. |
5 | | -var obj = currentObject = {}; |
6 | | -var ancestors = []; |
7 | | - |
8 | | -var options = {}; //configuration options |
9 | | - |
10 | | -function startElement(name, attrs) { |
11 | | - if (! (name in currentObject)) { |
12 | | - currentObject[name] = attrs; |
13 | | - } else if (! (currentObject[name] instanceof Array)) { |
14 | | - // Put the existing object in an array. |
15 | | - var newArray = [currentObject[name]]; |
16 | | - // Add the new object to the array. |
17 | | - newArray.push(attrs); |
18 | | - // Point to the new array. |
19 | | - currentObject[name] = newArray; |
20 | | - } else { |
21 | | - // An array already exists, push the attributes on to it. |
22 | | - currentObject[name].push(attrs); |
23 | | - } |
24 | | - |
25 | | - // Store the current (old) parent. |
26 | | - ancestors.push(currentObject); |
27 | | - |
28 | | - // We are now working with this object, so it becomes the current parent. |
29 | | - if (currentObject[name] instanceof Array) { |
30 | | - // If it is an array, get the last element of the array. |
31 | | - currentObject = currentObject[name][currentObject[name].length - 1]; |
32 | | - } else { |
33 | | - // Otherwise, use the object itself. |
34 | | - currentObject = currentObject[name]; |
35 | | - } |
36 | | -} |
37 | | - |
38 | | -function text(data) { |
39 | | - data = data.trim(); |
40 | | - if (!data.length) { |
41 | | - return; |
42 | | - } |
43 | | - currentObject['$t'] = data; |
44 | | -} |
45 | | - |
46 | | -function endElement(name) { |
47 | | - // This should check to make sure that the name we're ending |
48 | | - // matches the name we started on. |
49 | | - var ancestor = ancestors.pop(); |
50 | | - if (!options.reversible) { |
51 | | - if ((Object.keys(currentObject).length == 1) && ('$t' in currentObject)) { |
52 | | - if (ancestor[name] instanceof Array) { |
53 | | - //console.log("list-replacing $t in " + name); |
54 | | - ancestor[name].push(ancestor[name].pop()['$t']); |
55 | | - } else { |
56 | | - //console.log("replacing $t in " + name); |
57 | | - ancestor[name] = currentObject['$t']; |
58 | | - } |
59 | | - } else { |
60 | | - //console.log("final " + name + ":"); |
61 | | - //console.log(currentObject); |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - currentObject = ancestor; |
66 | | -} |
67 | | - |
68 | | - |
69 | | -module.exports.toJson = function(xml, _options) { |
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 | | - |
79 | | - options = { |
80 | | - object: false, |
81 | | - reversible: false |
82 | | - }; |
83 | | - |
84 | | - for (var opt in _options) { |
85 | | - options[opt] = _options[opt]; |
86 | | - } |
87 | | - |
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; |
95 | | - } |
96 | | - |
97 | | - return JSON.stringify(obj); |
98 | | -}; |
| 1 | +var exports = module.exports; |
99 | 2 |
|
| 3 | +exports.toJson = require('./xml2json'); |
| 4 | +exports.toXml = require('./json2xml'); |
0 commit comments