forked from johngeorgewright/angular-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-xml.js
55 lines (46 loc) · 1.47 KB
/
angular-xml.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
if (typeof(angular) !== 'undefined') {
angular
.module('xml', [])
.config(['$provide', function ($provide) {
$provide.factory('xmlHttpInterceptor', ['$q', 'xmlFilter', function ($q, xmlFilter) {
return {
response: function (response) {
if (response) {
response.xml = xmlFilter(response.data);
return response;
} else {
return $q.when(response);
}
}
};
}]);
}])
.factory('xmlParser', ['$window', function ($window) {
function MicrosoftXMLDOMParser() {
this.parser = new $window.ActiveXObject('Microsoft.XMLDOM');
}
MicrosoftXMLDOMParser.prototype.parse = function (input) {
this.parser.async = false;
return this.parser.loadXML(input);
};
function XMLDOMParser() {
this.parser = new $window.DOMParser();
}
XMLDOMParser.prototype.parse = function (input) {
return this.parser.parseFromString(input, 'text/xml');
};
if ($window.DOMParser) {
return new XMLDOMParser();
} else if ($window.ActiveXObject) {
return new MicrosoftXMLDOMParser();
} else {
throw new Error('Cannot parser XML in this environment.');
}
}])
.filter('xml', ['xmlParser', function (xmlParser) {
return function (input) {
var xmlDoc = xmlParser.parse(input);
return angular.element(xmlDoc);
};
}]);
}