Skip to content

Commit 584a710

Browse files
committed
initial changes for xml parser replacement
1 parent 01b0404 commit 584a710

3 files changed

Lines changed: 35 additions & 43 deletions

File tree

lib/helper.js

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
* Released under the MIT License
55
*/
66

7-
var jsonml = require('jsonml'),
7+
var xml2js = require('xml2js'),
88
url = require('url'),
99
os = require('os');
1010

11+
var parser = new xml2js.Parser({explicitArray: false});
12+
1113
var reNumber = /^[\.\+\-]?[\d\.]+$/,
1214
reInvalidDec = /(?:\.\d*){2,}/,
1315
reDec = /\./,
@@ -27,48 +29,33 @@ function parseNumber(s) {
2729
return reDec.test(s) ? parseFloat(s, 10) : parseInt(s, 10);
2830
}
2931

30-
function xmlToObj(xml) {
31-
var obj = {},
32-
33-
rec = function xmlToObjRecursion(a, o) {
34-
var val, newObj, i,
35-
len = a.length;
36-
37-
if (len === 2 && !o[a[0]]) {
38-
if (typeof a[1] !== 'object') {
39-
o[a[0]] = parseNumber(a[1]);
40-
} else {
41-
o[a[0]] = {};
42-
rec(a[1], o[a[0]]);
43-
}
44-
} else if (len >= 2) {
45-
if (o[a[0]]) {
46-
if (!(o[a[0]] instanceof Array)) {
47-
val = o[a[0]];
48-
o[a[0]] = [val];
49-
}
50-
if (len === 2) {
51-
return rec([o[a[0]].length, a[1]], o[a[0]]);
52-
} else {
53-
newObj = {};
54-
o[a[0]].push(newObj);
55-
}
56-
} else {
57-
o[a[0]] = {};
58-
}
59-
for (i = 1, len = a.length; i < len; i += 1) {
60-
rec(a[i], newObj || o[a[0]]);
61-
}
62-
} else if (len === undefined && typeof a === 'object') {
63-
Object.keys(a).forEach(function(key) {
64-
o[key] = a[key];
65-
});
32+
function normalizeObj(root) {
33+
if (typeof root === 'object') {
34+
Object.keys(root).forEach(function(key) {
35+
var value = root[key];
36+
if (typeof value === 'string') {
37+
if (value.length === 0 || value === '\n') {
38+
delete root[key];
39+
} else {
40+
root[key] = parseNumber(value);
6641
}
67-
};
42+
} else {
43+
normalizeObj(value);
44+
}
45+
});
46+
}
47+
}
6848

69-
rec(jsonml.parse(xml), obj);
49+
function xmlToObj(xml, callback) {
7050

71-
return obj;
51+
parser.parseString(xml, function (err, obj) {
52+
if (err) {
53+
callback(err);
54+
}
55+
56+
normalizeObj(obj);
57+
callback(undefined, obj);
58+
});
7259
}
7360

7461
function svToObj(delimiter, headers, sv) {

lib/webpagetest.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ function api(pathname, callback, query, options) {
156156
} else if (info.type === 'application/json') {
157157
data = JSON.parse(data);
158158
} else if (info.type === 'text/xml') {
159-
data = helper.xmlToObj(data);
159+
helper.xmlToObj(data, function(err, obj) {
160+
if (typeof callback === 'function') {
161+
callback.apply(this, [err, obj].concat(options.args));
162+
}
163+
return;
164+
});
160165
} else if (info.type === 'text/html') {
161166
data = {result: (reHTMLOutput.exec(data) || [])[1]};
162167
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
"node": ">=0.8.1"
3434
},
3535
"dependencies": {
36-
"jsonml": "~0.0.4",
3736
"commander": "~1.3.2",
38-
"mocha": "~1.12.0"
37+
"mocha": "~1.12.0",
38+
"xml2js": "~0.2.8"
3939
},
4040
"devDependencies": {
4141
"nock": "~0.19.0"

0 commit comments

Comments
 (0)