Skip to content

Commit defaba8

Browse files
committed
support parsing of xml strings
1 parent 19398ee commit defaba8

File tree

4 files changed

+100
-68
lines changed

4 files changed

+100
-68
lines changed

Gruntfile.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ module.exports = (grunt) ->
2828
require: true
2929
dev:
3030
options:
31-
ignores: ['*.min.js', 'js/*.min.js']
32-
src: ['*.js', 'js/*.js']
31+
ignores: ['*.min.js', 'src/*.min.js']
32+
src: ['*.js', 'src/*.js']
3333

3434
grunt.loadNpmTasks 'grunt-contrib-jshint'
3535
grunt.loadNpmTasks 'grunt-npm'

js/xml2json.js

Lines changed: 0 additions & 65 deletions
This file was deleted.

karma.conf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ module.exports = function(config) {
1313

1414
// list of files / patterns to load in the browser
1515
files: [
16-
'js/*.js',
16+
'lib/jquery/jquery.js',
17+
'src/*.js',
1718
'test/*.coffee'
1819
],
1920

src/xml2json.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* jQuery plugin to convert a given $.ajax response xml object to json.
3+
*
4+
* @example var json = $.xml2json(response);
5+
*/
6+
(function() {
7+
8+
// default options based on https://github.com/Leonidas-from-XIV/node-xml2js
9+
var defaultOptions = {
10+
attrkey: '$',
11+
charkey: '_'
12+
};
13+
14+
function parseXML(data) {
15+
var xml, tmp;
16+
if (!data || typeof data !== "string") {
17+
return null;
18+
}
19+
try {
20+
if (window.DOMParser) { // Standard
21+
tmp = new DOMParser();
22+
xml = tmp.parseFromString(data, "text/xml");
23+
} else { // IE
24+
xml = new ActiveXObject("Microsoft.XMLDOM");
25+
xml.async = "false";
26+
xml.loadXML(data);
27+
}
28+
} catch (e) {
29+
xml = undefined;
30+
}
31+
if (!xml || !xml.documentElement || xml.getElementsByTagName("parsererror").length) {
32+
throw new Error("Invalid XML: " + data);
33+
}
34+
return xml;
35+
}
36+
37+
/**w
38+
* Converts an xml response object from a $.ajax() call to a JSON object.
39+
*
40+
* @param xml
41+
*/
42+
function xml2json(xml, options) {
43+
options = options || defaultOptions;
44+
45+
if (typeof xml === 'string') {
46+
xml = parseXML(xml);
47+
}
48+
49+
var result = {};
50+
51+
for (var i in xml.childNodes) {
52+
var node = xml.childNodes[i];
53+
if (node.nodeType === 1) {
54+
var child = node.hasChildNodes() ? xml2json(node, options) : node.nodevalue;
55+
child = child === null ? {} : child;
56+
57+
if (result.hasOwnProperty(node.nodeName)) {
58+
// For repeating elements, cast/promote the node to array
59+
if (!(result[node.nodeName] instanceof Array)) {
60+
var tmp = result[node.nodeName];
61+
result[node.nodeName] = [];
62+
result[node.nodeName].push(tmp);
63+
}
64+
result[node.nodeName].push(child);
65+
} else {
66+
result[node.nodeName] = child;
67+
}
68+
69+
// Add attributes if any
70+
if (node.attributes.length > 0) {
71+
result[node.nodeName][options.attrkey] = {};
72+
child[options.attrkey] = {};
73+
for (var j in node.attributes) {
74+
var attribute = node.attributes.item(j);
75+
child[options.attrkey][attribute.nodeName] = attribute.value;
76+
}
77+
}
78+
79+
// Add element value
80+
if (node.childElementCount === 0 && node.textContent !== null && node.textContent !== "") {
81+
child[options.charkey] = node.textContent.trim();
82+
}
83+
}
84+
}
85+
86+
return result;
87+
}
88+
89+
if (typeof jQuery !== 'undefined') {
90+
jQuery.extend({xml2json: xml2json});
91+
} else if (typeof module !== 'undefined') {
92+
module.exports = xml2json;
93+
} else if (typeof window !== 'undefined') {
94+
window.xml2json = xml2json;
95+
}
96+
})();

0 commit comments

Comments
 (0)