angular-xml
XML module for AngularJS.
As of v2.0.0 this module relies on the X2JS module
Supplies the X2JS library as a service and provides a HTTP interceptor to convert all XML responses in to JSON.
Configuring the X2JS service
angular
.module('myMod', ['xml'])
.config(function (x2jsProvider) {
x2jsProvider.config = {
};
});
For any more information on how to configure and use the X2JS service, see their project.
Accessing the X2JS service
angular
.module('myMod', ['xml'])
.factory('someFactory', function (x2js) {
var xmlDoc = x2js.json2xml(
{
MyRoot : {
MyChild : 'my_child_value',
MyAnotherChild: 10,
MyArray : [ 'test', 'test2' ],
MyArrayRecords : [
{
ttt : 'vvvv'
},
{
ttt : 'vvvv2'
}
]
}
}
);
});
Read the docs on how to use it.
Using the HTTP interceptor
The HTTP interceptor will convert all your XML responses in to a JavaScript Object.
<blogs>
<blog name="my first blog" id="1"/>
</blogs>
angular
.module('blogs', ['xml'])
.config(function ($httpProvider) {
$httpProvider.interceptors.push('xmlHttpInterceptor');
})
.controller(function ($scope, $http) {
$http.get('blogs.xml').success(function (data) {
$scope.blogs = data.blogs.blog;
});
});
<!doctype html>
<html lang="en" ng-app="blogs">
<head>
<title>Blogs</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script src="bower_components/x2js/xml2json.min.js"></script>
<script src="bower_components/angular-xml/angular-xml.min.js"></script>
<script src="blogs.js"></script>
</head>
<body ng-controller="BlogsCtrl">
<ul>
<li ng-repeat="blog in blogs">
{{blog._id}} - {{blog._name}}
</li>
</ul>
</body>
</html>
Installation
First acquire the X2JS library (this comes bundled with the bower option described next)
Then there are 3 optoins:
- Download the latest tag.
- Use bower:
bower i --save angular-xml
- Or use jsDelivr CDN:
//cdn.jsdelivr.net/npm/angular-xml@2.2.2/angular-xml.min.js
Contributing
To contribute to the project take the following steps:
- Fork the project.
- Create a branch specific for your change(s).
- Submit a pull request to my master branch and we can begin the process of merging.
When submitting, please make sure your code is covered by tests.
Tests
The unit tests run with Karma and the E2E tests run with Protractor.
Update the webdriver
npm run update-webdriver
Run the test suite
npm test
Or, to watch your files and test automatically:
npm run dev
Compiling
The source file angular-xml.js
can be minifed and checked for problems using a grunt command. First make sure you have installed all npm dependencies npm i
. Then run grunt
.
Git Hooks
There is a git hook available for shell environments that will automatically lint, test and compile the xml module when commiting it. To use it simply link it in to the git hook directory.
npm run hook-git
# And... of you want to remove the hook
npm run unhook-git
Now when you change the angular-xml.js
file and commit it, it will be linted, tested and if all is OK, then compiled and the minified version wil be added to your commit.