It provides 3 helpers:
A parser to turn an XML string in to a DOM object.
function MyCtrl(xmlParser) { var domElement = xmlParser.parse('<blogs><blog name="my first blog" id="1"/></blogs>'); console.log(domElement); // => #document }
A filter to convert an XML string in to an Angular element.
function MyCtrl(xmlFilter) { var xml = xmlFilter('<blogs><blog name="my first blog" id="1"/></blogs>'); console.log(xml.find('blog')); // => [blog#1] }
A HTTP interceptor to turn all you responses in to an Angular element.
angular .module('blogs', ['xml']) .config(function ($httpProvider) { $httpProvider.responseInterceptors.push('xmlHttpInterceptor'); }) .controller('Blogs', function BlogsCtrl($scope, $http) { $scope.blogs = []; $http.get('blogs.xml').then(function (response) { var blogs = [], els = response.xml.find('blog'), blog, i; for (i = 0; i < els.length; i += 1) { blog = angular.element(els[i]); blogs.push({ name: blog.attr('name'), id: blog.attr('id') }); } $scope.blogs = blogs; }); });
Installation
There are 3 options:
- Download the latest tag.
- Use bower:
bower i --save angular-xml
- Or use jsDelivr CDN: //cdn.jsdelivr.net/angular.xml/0.1.3/angular-xml.min.js