Last active
October 7, 2015 03:54
-
-
Save otarim/3dd8d3d45b8d0134e034 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| angular.module('ngParser') | |
| .factory('xmlParser', function() { | |
| var parse = function(doc) { | |
| var ret = {} | |
| if (doc.attributes) {; | |
| [].forEach.call(doc.attributes, function(node) { | |
| ret[node.nodeName] = node.value | |
| }) | |
| } | |
| if (doc.childNodes.length) {; | |
| [].forEach.call(doc.childNodes, function(node) { | |
| if (ret[node.tagName]) { | |
| ret[node.tagName] = [].concat(ret[node.tagName]) | |
| ret[node.tagName].push(parse(node)) | |
| } else { | |
| ret[node.tagName] = parse(node) | |
| } | |
| }) | |
| } | |
| return ret | |
| } | |
| return function(xmlText) { | |
| var parser = new DOMParser(), | |
| xmlDoc = parser.parseFromString(xmlText, 'text/xml') | |
| return parse(xmlDoc) | |
| } | |
| }) | |
| .factory('jsonParser', function(_) { | |
| return function toXml(input){ | |
| if (_.isArray(input)) { | |
| return _.map(input, toXml).join('') | |
| } else if (_.isObject(input)) { | |
| return _.map(input, function(value, key) { | |
| return '<' + key + '>' + toXml(value) + '</' + key + '>' | |
| }).join('') | |
| } | |
| return '<![CDATA[' + input + ']]>' | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment