Permalink
Browse files

First version (v0.1.0) of data2xml

  • Loading branch information...
0 parents commit 2706bfcf0d3d8d008d045194c4610dc46b0367cf @chilts committed Nov 1, 2011
Showing with 418 additions and 0 deletions.
  1. +4 −0 .npmignore
  2. +29 −0 LICENSE
  3. +85 −0 README.md
  4. +7 −0 bin/test.sh
  5. +84 −0 data2xml.js
  6. +36 −0 package.json
  7. +55 −0 test/basics.js
  8. +26 −0 test/load.js
  9. +92 −0 test/xml-generation.js
@@ -0,0 +1,4 @@
+*~
+*.swp
+npm-debug.log
+node_modules
29 LICENSE
@@ -0,0 +1,29 @@
+-------------------------------------------------------------------------------
+
+This software is published under the MIT license as published here:
+
+* http://opensource.org/licenses/MIT
+
+-------------------------------------------------------------------------------
+
+Copyright 2011 Apps Attic Ltd. All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+of the Software, and to permit persons to whom the Software is furnished to do
+so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
+-------------------------------------------------------------------------------
@@ -0,0 +1,85 @@
+
+
+
+ var data = {
+ _attr : {
+ xmlns : 'https://route53.amazonaws.com/doc/2011-05-05/',
+ },
+ ChangeBatch : {
+ Comment : 'This is a comment',
+ Changes : {
+ Change : [
+ {
+ Action : 'CREATE',
+ ResourceRecordSet : {
+ Name : 'www.example.com',
+ Type : 'A',
+ TTL : 300,
+ ResourceRecords : {
+ ResourceRecord : [
+ {
+ Value : '192.0.2.1'
+ }
+ ]
+ }
+ },
+ ],
+ },
+ },
+ };
+
+ var xml = data2xml('ChangeResourceRecordSetsRequest', data);
+
+Examples
+--------
+
+ var data2xml = require('data2xml')';
+
+ data2xml('TopLevelElement', {
+ _attr : { xmlns : 'http://appsattic.com/xml/namespace' }
+ SimpleElement : 'A simple element',
+ ComplexElement : {
+
+ }
+ });
+
+ =>
+
+
+Why data2xml
+------------
+
+Just looking at the XML modules out there I found that the data structure I had to create to get some XML out of the
+other end was just horrible.
+
+
+
+
+What data2xml does
+------------------
+
+data2xml converts data structures into XML. It's that simple.
+
+
+
+
+What data2xml doesn't do
+------------------------
+
+Data2Xml is designed to be an easy way to get from a data structure to XML. Various other JavaScript to XML modules try
+and do everything which means that the interface is pretty dire. If you just want an easy way to get XML using a sane
+data structure, then this module is for you.
+
+To decide this, you need to know what this module doesn't do. It doesn't deal with:
+
+* mixed type elements (such as <markup>Hello <strongly>World</strongly></markup>)
+* pretty formatting - after all, you're probably sending this XML to another machine
+* CDATA elements ... though I probably _should_ add this (somehow)
+* data objects which are (or have) functions
+* ordered elements - if you pass me an object, it's members will be output in an order defined by 'for m in object'
+* comments
+* processing instructions
+* entity references
+* all the other stuff you don't care about when dealing with data
+
+(Ends)
@@ -0,0 +1,7 @@
+#!/bin/sh
+## --------------------------------------------------------------------------------------------------------------------
+
+# run our unit tests
+./node_modules/tap/bin/tap.js test/*.js
+
+## --------------------------------------------------------------------------------------------------------------------
@@ -0,0 +1,84 @@
+// --------------------------------------------------------------------------------------------------------------------
+//
+// data2xml.js - A data to XML converter with a nice interface (for NodeJS).
+//
+// Copyright (c) 2011 AppsAttic Ltd - http://www.appsattic.com/
+// Written by Andrew Chilton <chilts@appsattic.com>
+//
+// License: http://opensource.org/licenses/MIT
+//
+// --------------------------------------------------------------------------------------------------------------------
+
+var xmlHeader = '<?xml version="1.0" encoding="utf-8"?>\n';
+
+function entitify(str) {
+ str = '' + str;
+ str = str
+ .replace(/&/g, '&amp;')
+ .replace(/</g,'&lt;')
+ .replace(/>/g,'&gt;')
+ .replace(/'/g, '&apos;')
+ .replace(/"/g, '&quot;');
+ return str;
+}
+
+function makeStartTag(name, attr) {
+ attr = attr || {};
+ var tag = '<' + name;
+ for(var a in attr) {
+ tag += ' ' + a + '="' + entitify(attr[a]) + '"';
+ }
+ tag += '>';
+ return tag;
+}
+
+function makeEndTag(name) {
+ return '</' + name + '>';
+}
+
+function makeElement(name, data) {
+ var element = '';
+ if ( Array.isArray(data) ) {
+ data.forEach(function(v) {
+ element += makeElement(name, v);
+ });
+ return element;
+ }
+ else if ( typeof data === 'object' ) {
+ element += makeStartTag(name, data._attr);
+ if ( data._value ) {
+ element += entitify(data._value);
+ }
+ else {
+ for (var el in data) {
+ if ( el === '_attr' ) {
+ continue;
+ }
+ element += makeElement(el, data[el]);
+ }
+ }
+ element += makeEndTag(name);
+ return element;
+ }
+ else {
+ // a piece of data on it's own can't have attributes
+ return makeStartTag(name) + entitify(data) + makeEndTag(name);
+ }
+ throw "Unknown data " + data;
+}
+
+var data2xml = function(name, data) {
+ var xml = xmlHeader;
+ xml += makeElement(name, data);
+ return xml;
+};
+
+// --------------------------------------------------------------------------------------------------------------------
+
+exports.entitify = entitify;
+exports.makeStartTag = makeStartTag;
+exports.makeEndTag = makeEndTag;
+exports.makeElement = makeElement;
+exports.data2xml = data2xml;
+
+// --------------------------------------------------------------------------------------------------------------------
@@ -0,0 +1,36 @@
+{
+ "name": "data2xml",
+ "description": "A data to XML converter with a nice interface (for NodeJS).",
+ "version": "0.1.0",
+ "author": {
+ "name":"Andrew Chilton",
+ "email":"chilts@appsattic.com",
+ "url":"http://www.chilts.org/"
+ },
+ "homepage": "https://github.com/appsattic/node-data2xml",
+ "contributors": [],
+ "devDependencies": {
+ "tap": ">= 0.0.11"
+ },
+ "dependencies": {},
+ "bundledDependencies": [],
+ "main": "data2xml.js",
+ "directories": {},
+ "engines": {
+ "node": "*"
+ },
+ "repository": {
+ "type" : "git",
+ "url": "git://github.com/appsattic/node-data2xml.git"
+ },
+ "licenses": [{
+ "type": "MIT",
+ "url": "http://github.com/appsattic/node-data2xml/raw/master/LICENSE"
+ }],
+ "keywords": [
+ "data", "xml", "data2xml", "datatoxml", "js2xml", "jstoxml", "json2xml", "jsontoxml"
+ ],
+ "scripts" : {
+ "test" : "./bin/test.sh"
+ }
+}
@@ -0,0 +1,55 @@
+// --------------------------------------------------------------------------------------------------------------------
+//
+// basics.js - tests for node-data2xml
+//
+// Copyright (c) 2011 AppsAttic Ltd - http://www.appsattic.com/
+// Written by Andrew Chilton <chilts@appsattic.com>
+//
+// License: http://opensource.org/licenses/MIT
+//
+// --------------------------------------------------------------------------------------------------------------------
+
+var tap = require("tap"),
+ test = tap.test,
+ plan = tap.plan;
+var data2xml = require('../data2xml');
+
+// --------------------------------------------------------------------------------------------------------------------
+
+test("some simple entities", function (t) {
+ var test1 = data2xml.entitify('<hello>');
+ var exp1 = '&lt;hello&gt;';
+ t.equal(test1, exp1, 'LT and GT entitified correctly');
+
+ var test2 = data2xml.entitify('\'&\"');
+ var exp2 = '&apos;&amp;&quot;';
+ t.equal(test2, exp2, 'other entities');
+
+ t.end();
+});
+
+test("making some elements", function (t) {
+ var test1 = data2xml.makeStartTag('tagme');
+ var exp1 = '<tagme>';
+ t.equal(test1, exp1, 'simple start tag');
+
+ var test2 = data2xml.makeEndTag('tagme');
+ var exp2 = '</tagme>';
+ t.equal(test2, exp2, 'simple end tag');
+
+ var test3 = data2xml.makeStartTag('tagme', { attr : 'value' });
+ var exp3 = '<tagme attr="value">';
+ t.equal(test3, exp3, '1) complex start tag');
+
+ var test4 = data2xml.makeStartTag('tagme', { attr : '<anothertag>' });
+ var exp4 = '<tagme attr="&lt;anothertag&gt;">';
+ t.equal(test4, exp4, '2) complex start tag');
+
+ var test5 = data2xml.makeStartTag('tagme', { attr1 : '<anothertag>', attr2 : 'val2' });
+ var exp5 = '<tagme attr1="&lt;anothertag&gt;" attr2="val2">';
+ t.equal(test5, exp5, '3) complex start tag');
+
+ t.end();
+});
+
+// --------------------------------------------------------------------------------------------------------------------
@@ -0,0 +1,26 @@
+// --------------------------------------------------------------------------------------------------------------------
+//
+// load.js - tests for node-data2xml
+//
+// Copyright (c) 2011 AppsAttic Ltd - http://www.appsattic.com/
+// Written by Andrew Chilton <chilts@appsattic.com>
+//
+// License: http://opensource.org/licenses/MIT
+//
+// --------------------------------------------------------------------------------------------------------------------
+
+var tap = require("tap"),
+ test = tap.test,
+ plan = tap.plan;
+var data2xml;
+
+// --------------------------------------------------------------------------------------------------------------------
+
+test("load data2xml", function (t) {
+ data2xml = require("../data2xml");
+ t.ok(data2xml, "package loaded");
+
+ t.end();
+});
+
+// --------------------------------------------------------------------------------------------------------------------
Oops, something went wrong.

0 comments on commit 2706bfc

Please sign in to comment.