• Stars
    star
    177
  • Rank 215,985 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

XML module for AngularJS

Codecov Build Status NPM Version Bower Dependencies License Greenkeeper badge

Sauce Test Status

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 = {
      /*
      escapeMode               : true|false - Escaping XML characters. Default is true from v1.1.0+
      attributePrefix          : "<string>" - Prefix for XML attributes in JSon model. Default is "_"
      arrayAccessForm          : "none"|"property" - The array access form (none|property). Use this property if you want X2JS generates an additional property <element>_asArray to access in array form for any XML element. Default is none from v1.1.0+
      emptyNodeForm            : "text"|"object" - Handling empty nodes (text|object) mode. When X2JS found empty node like <test></test> it will be transformed to test : '' for 'text' mode, or to Object for 'object' mode. Default is 'text'
      enableToStringFunc       : true|false - Enable/disable an auxiliary function in generated JSON objects to print text nodes with text/cdata. Default is true
      arrayAccessFormPaths     : [] - Array access paths. Use this option to configure paths to XML elements always in "array form". You can configure beforehand paths to all your array elements based on XSD or your knowledge. Every path could be a simple string (like 'parent.child1.child2'), a regex (like /.*\.child2/), or a custom function. Default is empty
      skipEmptyTextNodesForObj : true|false - Skip empty text tags for nodes with children. Default is true.
      stripWhitespaces         : true|false - Strip whitespaces (trimming text nodes). Default is true.
      datetimeAccessFormPaths  : [] - Datetime access paths. Use this option to configure paths to XML elements for "datetime form". You can configure beforehand paths to all your array elements based on XSD or your knowledge. Every path could be a simple string (like 'parent.child1.child2'), a regex (like /.*\.child2/), or a custom function. Default is empty
      */
    };
  });

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.xml -->
<blogs>
  <blog name="my first blog" id="1"/>
</blogs>
// blogs.js
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:

  1. Download the latest tag.
  2. Use bower: bower i --save angular-xml
  3. Or use jsDelivr CDN: //cdn.jsdelivr.net/npm/[email protected]/angular-xml.min.js

Contributing

To contribute to the project take the following steps:

  1. Fork the project.
  2. Create a branch specific for your change(s).
  3. 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.

More Repositories

1

grunt-http

Sends a HTTP request and deals with the response.
JavaScript
44
star
2

runtyping

Generate runtype validation from static types & JSON schema.
TypeScript
43
star
3

atom-rainbow

Rainbow parenthesis for the Atom Editor
JavaScript
17
star
4

promdash

ES6 Promise implementation of lodash
JavaScript
15
star
5

angular-ie7-support

IE7 support module for angular
JavaScript
14
star
6

hap

An extension to the Node.js events.EventEmitter.
JavaScript
11
star
7

wamp-cli

A CLI tool for building applications with the Web Application Messaging Protocol
TypeScript
8
star
8

angular-auto-value

Initiating models via the value attribute.
JavaScript
7
star
9

google-ad-manager-api

Typed Google Ad Manager API
TypeScript
2
star
10

hubot-chuck-norris

Hubot extension for chuck norris quotes
CoffeeScript
1
star
11

zod-guard

Guard functions for Zod types
TypeScript
1
star
12

pass.js

JavaScript interface for pass (passwordstore)
TypeScript
1
star
13

coffee-views

OO templates for Node.js
CoffeeScript
1
star
14

npm-check-git

Checks if a git dependency requires an update
TypeScript
1
star
15

regex-action

Applies a regular expression to an input.
1
star
16

vim-config

My personal VIM configuration.
Vim Script
1
star
17

module-index

A Node.js module to include all of a module's siblings as exports.
JavaScript
1
star
18

ts-module

TypeScript NPM package template
JavaScript
1
star
19

targetd

A speedy, highly extensible, typed, in-memory data store built in Node.js & Typescript.
TypeScript
1
star
20

chai-match

Advanced RegExp assertions for the Chai.js framework
JavaScript
1
star
21

hot-press

Event lifecycle management in JavaScript
JavaScript
1
star
22

js-util

Language utilities for JavaScript
TypeScript
1
star
23

node-child-proc

An extension to the child_process module that fixes problems like windows commands.
1
star
24

grunter

Grunt (<0.4.0) that works in windows
JavaScript
1
star
25

taxon

Creates SVN branches and tags for a collaboration of projects at the same time.
Shell
1
star
26

Semantic-Suite

A semantic testing suite
CoffeeScript
1
star
27

coffee-views-pure

A coffee-views extension to add YUI's pure functionality
CoffeeScript
1
star