├── .gitignore ├── .gitlab-ci.yml ├── .jshintrc ├── .npmignore ├── .travis.yml ├── LICENSE ├── ReadMe.md ├── data2xml.js ├── examples ├── complex.js └── simple.js ├── package-lock.json ├── package.json └── test ├── basics.js ├── config-2.js ├── config.js ├── doc-xml.js ├── load.js └── xml-generation.js /.gitignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | 3 | node_modules 4 | npm-debug.log 5 | *~ 6 | -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | cache: 2 | paths: 3 | - node_modules 4 | 5 | test:unit-node8: 6 | image: node:8 7 | script: 8 | - node --version 9 | - npm --version 10 | - npm install 11 | - npm test 12 | 13 | test:unit-node10: 14 | image: node:10 15 | script: 16 | - node --version 17 | - npm --version 18 | - npm install 19 | - npm test 20 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "es5": true, 3 | "indent": 4, 4 | "eqeqeq": true, 5 | "curly": true, 6 | "camelcase": true, 7 | "quotmark": "single" 8 | } 9 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.swp 3 | npm-debug.log 4 | node_modules 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.12" 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | 3 | This software is published under the MIT license as published here: 4 | 5 | * http://opensource.org/licenses/MIT 6 | 7 | ------------------------------------------------------------------------------- 8 | 9 | Copyright 2011-2012 Apps Attic Ltd. All rights reserved. 10 | Copyright 2013-2014 Andrew Chilton. All rights reserved. 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy of 13 | this software and associated documentation files (the "Software"), to deal in 14 | the Software without restriction, including without limitation the rights to 15 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 16 | of the Software, and to permit persons to whom the Software is furnished to do 17 | so, subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | 30 | ------------------------------------------------------------------------------- 31 | -------------------------------------------------------------------------------- /ReadMe.md: -------------------------------------------------------------------------------- 1 | data2xml is a data to XML converter with a nice interface (for NodeJS). 2 | 3 | [![Build Status](https://secure.travis-ci.org/chilts/data2xml.png?branch=master)](http://travis-ci.org/chilts/data2xml) 4 | 5 | [![NPM](https://nodei.co/npm/data2xml.png?downloads=true)](https://nodei.co/npm/data2xml/) 6 | 7 | Installation 8 | ------------ 9 | 10 | The easiest way to get it is via npm: 11 | 12 | npm install --save data2xml 13 | 14 | Info and Links: 15 | 16 | * npm info data2xml 17 | * https://npmjs.org/package/data2xml 18 | * https://github.com/chilts/data2xml/ 19 | 20 | Stable 21 | ------ 22 | 23 | This package hasn't been significantly changed for a while. It is considered stable (it is not stagnant). Please use 24 | with all your might and enjoy it. 25 | 26 | Note: this package doesn't depend on any others (except testing in development). This is on purpose so that it doesn't 27 | have it's foundation move under it's feet. 28 | 29 | Synopsis 30 | -------- 31 | 32 | ``` 33 | var data2xml = require('data2xml'); 34 | 35 | var convert = data2xml(); 36 | 37 | var xml1 = convert('Message', 'Hello, World!'); 38 | console.log(xml1); 39 | // -> 40 | // 41 | // Hello, World! 42 | 43 | var xml2 = convert('Message', { 44 | Text: 'Hello, World!' 45 | }); 46 | console.log(xml2); 47 | // -> 48 | // 49 | // Hello, World! 50 | ``` 51 | 52 | Examples 53 | -------- 54 | 55 | When loading up data2xml, you can't use it immediately. Firstly you need to call the data2xml() function which will 56 | return a function you can use. The reason for this is so that you can encapsulate any configuration options into one 57 | call and not have to pass that in every time. 58 | 59 | e.g. 60 | 61 | ``` 62 | var data2xml = require('data2xml'); 63 | var convert = data2xml(); // or data2xml({}) 64 | ``` 65 | 66 | Note: in each example, I am leaving out the XML declaration (controlled by the `xmlDecl` option). I 67 | am also pretty printing the output - the package doesn't do this for you! 68 | 69 | ```js 70 | var convert = require('data2xml')({ xmlDecl : false }); 71 | 72 | convert( 73 | 'TopLevelElement', 74 | { 75 | _attr : { xmlns : 'http://chilts.org/xml/namespace' } 76 | SimpleElement : 'A simple element', 77 | ComplexElement : { 78 | A : 'Value A', 79 | B : 'Value B', 80 | }, 81 | } 82 | ); 83 | ``` 84 | 85 | Will produce: 86 | 87 | ```xml 88 | 89 | A simple element 90 | 91 | Value A 92 | Value B 93 | 94 | 95 | ``` 96 | 97 | If you want an element containing data you can do it one of two ways. A simple piece of data will work, but if you want 98 | attributes you need to specify the value in the element object. You can also specify a CDATA element too. 99 | 100 | ```js 101 | convert( 102 | 'TopLevelElement', 103 | { 104 | _attr : { xmlns : 'http://chilts.org/xml/namespace' } 105 | SimpleData : 'Simple Value', 106 | ComplexData : { 107 | _attr : { type : 'colour' }, 108 | _value : 'White', 109 | }, 110 | CData : { 111 | _cdata : 'This isbold.', 112 | }, 113 | } 114 | ); 115 | ``` 116 | 117 | Will produce: 118 | 119 | ```xml 120 | 121 | Simple Value 122 | White 123 | bold.]]> 124 | 125 | ``` 126 | 127 | You can also specify which properties your attributes and values are in (using the same example as above): 128 | 129 | ```js 130 | var convert = require('data2xml')({ attrProp : '@', valProp : '#', cdataProp : '%' }); 131 | convert( 132 | 'TopLevelElement', 133 | { 134 | _attr : { xmlns : 'http://chilts.org/xml/namespace' } 135 | SimpleData : 'Simple Value', 136 | ComplexData : { 137 | '@' : { type : 'colour' }, 138 | '#' : 'White', 139 | }, 140 | CData : { 141 | '%' : 'This is bold.', 142 | }, 143 | }); 144 | ``` 145 | 146 | Will produce: 147 | 148 | ```xml 149 | 150 | Simple Value 151 | White 152 | bold.]]> 153 | 154 | ``` 155 | 156 | You can also specify what you want to do with undefined or null values. Choose between 'omit' (the default), 'empty' or 157 | 'closed'. 158 | 159 | ```js 160 | var convert = require('data2xml')({ 'undefined' : 'empty', 'null' : 'closed', }); 161 | convert( 162 | 'TopLevelElement', 163 | { 164 | SimpleData : 'Simple Value', 165 | ComplexData : { 166 | '_attr' : { type : 'colour' }, 167 | '_value' : 'White', 168 | }, 169 | Undefined : undefined, 170 | Null : null, 171 | }); 172 | ``` 173 | 174 | Will produce: 175 | 176 | ```xml 177 | 178 | Simple Value 179 | White 180 | 181 | 182 | 183 | ``` 184 | 185 | If you want an array, just put one in there: 186 | 187 | ```js 188 | convert('TopLevelElement', { 189 | MyArray : [ 190 | 'Simple Value', 191 | { 192 | _attr : { type : 'colour' }, 193 | _value : 'White', 194 | } 195 | ], 196 | }); 197 | ``` 198 | 199 | Will produce: 200 | 201 | ```xml 202 | 203 | Simple Value 204 | White 205 | 206 | ``` 207 | 208 | You can also enclose values in CDATA, by using `_cdata` in place of `_value`: 209 | 210 | ```js 211 | convert( 212 | 'TopLevelElement', 213 | { 214 | SimpleData : 'Simple Value', 215 | ComplexData : { 216 | _attr : { type : 'colour' }, 217 | _cdata : 'White', 218 | } 219 | } 220 | ); 221 | ``` 222 | 223 | Will produce: 224 | 225 | ```xml 226 | 227 | Simple Value 228 | White]]> 229 | 230 | ``` 231 | 232 | You can change the doctype declaration at initialization: 233 | 234 | ``` 235 | var data2xml = require('data2xml'); 236 | var convert = data2xml({xmlHeader: '\n'}); 237 | 238 | convert(…); 239 | ``` 240 | 241 | Why data2xml 242 | ------------ 243 | 244 | 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 245 | end was not very nice, nor very easy to create. This module is designed so that you can take any plain old data 246 | structure in one end and get an XML representation out of the other. 247 | 248 | In some cases you need to do something a little special (rather than a lot special) but these are for slightly more 249 | tricky XML representations. 250 | 251 | Also, I wanted a really simple way to convert data structures in NodeJS into an XML representation for the Amazon Web 252 | Services within node-awssum. This seemed to be the nicest way to do it (after trying the other js to xml modules). 253 | 254 | What data2xml does 255 | ------------------ 256 | 257 | data2xml converts data structures into XML. It's that simple. No need to worry! 258 | 259 | What data2xml doesn't do 260 | ------------------------ 261 | 262 | Data2Xml is designed to be an easy way to get from a data structure to XML. Various other JavaScript to XML modules try 263 | and do everything which means that the interface is pretty dire. If you just want an easy way to get XML using a sane 264 | data structure, then this module is for you. 265 | 266 | To decide this, you need to know what this module doesn't do. It doesn't deal with: 267 | 268 | * mixed type elements (such as `Hello World`) 269 | * pretty formatting - after all, you're probably sending this XML to another machine 270 | * data objects which are (or have) functions 271 | * ordered elements - if you pass me an object, it's members will be output in an order defined by 'for m in object' 272 | * comments 273 | * processing instructions 274 | * entity references 275 | * all the other stuff you don't care about when dealing with data 276 | 277 | ## Author ## 278 | 279 | ``` 280 | $ npx chilts 281 | 282 | ╒════════════════════════════════════════════════════╕ 283 | │ │ 284 | │ Andrew Chilton (Personal) │ 285 | │ ------------------------- │ 286 | │ │ 287 | │ Email : andychilton@gmail.com │ 288 | │ Web : https://chilts.org │ 289 | │ Twitter : https://twitter.com/andychilton │ 290 | │ GitHub : https://github.com/chilts │ 291 | │ GitLab : https://gitlab.org/chilts │ 292 | │ │ 293 | │ Apps Attic Ltd (My Company) │ 294 | │ --------------------------- │ 295 | │ │ 296 | │ Email : chilts@appsattic.com │ 297 | │ Web : https://appsattic.com │ 298 | │ Twitter : https://twitter.com/AppsAttic │ 299 | │ GitLab : https://gitlab.com/appsattic │ 300 | │ │ 301 | │ Node.js / npm │ 302 | │ ------------- │ 303 | │ │ 304 | │ Profile : https://www.npmjs.com/~chilts │ 305 | │ Card : $ npx chilts │ 306 | │ │ 307 | ╘════════════════════════════════════════════════════╛ 308 | ``` 309 | 310 | # License # 311 | 312 | MIT - http://chilts.mit-license.org/2012/ 313 | 314 | (Ends) 315 | -------------------------------------------------------------------------------- /data2xml.js: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // data2xml.js - A data to XML converter with a nice interface (for NodeJS). 4 | // 5 | // Copyright (c) 2011 Andrew Chilton - http://chilts.org/ 6 | // Written by Andrew Chilton 7 | // 8 | // License: http://opensource.org/licenses/MIT 9 | // 10 | // -------------------------------------------------------------------------------------------------------------------- 11 | 12 | var valid = { 13 | 'omit' : true, // no element is output : '' 14 | 'empty' : true, // an empty element is output : '' 15 | 'closed' : true // a closed element is output : '' 16 | }; 17 | 18 | var defaults = { 19 | 'attrProp' : '_attr', 20 | 'valProp' : '_value', 21 | 'undefined' : 'omit', 22 | 'null' : 'omit', 23 | 'xmlDecl' : true, 24 | 'cdataProp' : '_cdata' 25 | }; 26 | 27 | var xmlHeader = '\n'; 28 | 29 | module.exports = function(opts) { 30 | opts = opts || {}; 31 | 32 | opts.attrProp = opts.attrProp || defaults.attrProp; 33 | opts.valProp = opts.valProp || defaults.valProp; 34 | opts.cdataProp = opts.cdataProp || defaults.cdataProp; 35 | opts.xmlHeader = opts.xmlHeader || xmlHeader; 36 | 37 | if (typeof opts.xmlDecl === 'undefined') { 38 | opts.xmlDecl = defaults.xmlDecl; 39 | } 40 | 41 | if ( opts['undefined'] && valid[opts['undefined']] ) { 42 | // nothing, this is fine 43 | } 44 | else { 45 | opts['undefined'] = defaults['undefined']; 46 | } 47 | if ( opts['null'] && valid[opts['null']] ) { 48 | // nothing, this is fine 49 | } 50 | else { 51 | opts['null'] = defaults['null']; 52 | } 53 | 54 | return function(name, data) { 55 | var xml = opts.xmlDecl ? opts.xmlHeader : ''; 56 | xml += makeElement(name, data, opts); 57 | return xml; 58 | }; 59 | }; 60 | 61 | function entitify(str) { 62 | str = '' + str; 63 | str = str 64 | .replace(/&/g, '&') 65 | .replace(//g,'>') 67 | .replace(/'/g, ''') 68 | .replace(/"/g, '"'); 69 | return str; 70 | } 71 | 72 | function makeElementAttrs(attr) { 73 | var attributes = ''; 74 | for(var a in attr) { 75 | attributes += ' ' + a + '="' + entitify(attr[a]) + '"'; 76 | } 77 | return attributes; 78 | } 79 | 80 | function makeStartTag(name, attr) { 81 | attr = attr || {}; 82 | var tag = '<' + name; 83 | tag += makeElementAttrs(attr); 84 | tag += '>'; 85 | return tag; 86 | } 87 | 88 | function makeClosedElement(name, attr) { 89 | attr = attr || {}; 90 | var tag = '<' + name; 91 | tag += makeElementAttrs(attr); 92 | tag += '/>'; 93 | return tag; 94 | } 95 | 96 | function undefinedElement(name, attr, opts) { 97 | if ( opts['undefined'] === 'omit' ) { 98 | return ''; 99 | } 100 | if ( opts['undefined'] === 'empty' ) { 101 | return makeStartTag(name, attr) + makeEndTag(name); 102 | } 103 | else if ( opts['undefined'] === 'closed' ) { 104 | return makeClosedElement(name, attr); 105 | } 106 | 107 | } 108 | 109 | function nullElement(name, attr, opts) { 110 | if ( opts['null'] === 'omit' ) { 111 | return ''; 112 | } 113 | if ( opts['null'] === 'empty' ) { 114 | return makeStartTag(name, attr) + makeEndTag(name); 115 | } 116 | else if ( opts['null'] === 'closed' ) { 117 | return makeClosedElement(name, attr); 118 | } 119 | } 120 | 121 | function makeEndTag(name) { 122 | return ''; 123 | } 124 | 125 | function makeElement(name, data, opts) { 126 | var cdataRegExp = /]]\>/g; 127 | 128 | var element = ''; 129 | if ( Array.isArray(data) ) { 130 | data.forEach(function(v) { 131 | element += makeElement(name, v, opts); 132 | }); 133 | return element; 134 | } 135 | else if ( typeof data === 'undefined' ) { 136 | return undefinedElement(name, null, opts); 137 | } 138 | else if ( data === null ) { 139 | return nullElement(name, null, opts); 140 | } 141 | else if ( typeof data === 'object' ) { 142 | var valElement; 143 | if (data.hasOwnProperty(opts.valProp)) { 144 | valElement = data[opts.valProp]; 145 | 146 | if (typeof valElement === 'undefined') { 147 | return undefinedElement(name, data[opts.attrProp], opts); 148 | } 149 | 150 | if (valElement === null) { 151 | return nullElement(name, data[opts.attrProp], opts); 152 | } 153 | } 154 | element += makeStartTag(name, data[opts.attrProp]); 155 | 156 | if (valElement) { 157 | element += entitify(valElement); 158 | } 159 | else if ( data[opts.cdataProp] ) { 160 | element += '') + ']]>'; 161 | } 162 | 163 | for (var el in data) { 164 | if ( el === opts.attrProp || el === opts.valProp || el === opts.cdataProp) { 165 | continue; 166 | } 167 | element += makeElement(el, data[el], opts); 168 | } 169 | 170 | element += makeEndTag(name); 171 | 172 | return element; 173 | } 174 | else { 175 | // a piece of data on it's own can't have attributes 176 | return makeStartTag(name) + entitify(data) + makeEndTag(name); 177 | } 178 | throw 'Unknown data ' + data; 179 | } 180 | 181 | // -------------------------------------------------------------------------------------------------------------------- 182 | 183 | module.exports.makeStartTag = makeStartTag; 184 | module.exports.makeEndTag = makeEndTag; 185 | module.exports.makeElement = makeElement; 186 | module.exports.entitify = entitify; 187 | 188 | // -------------------------------------------------------------------------------------------------------------------- 189 | 190 | -------------------------------------------------------------------------------- /examples/complex.js: -------------------------------------------------------------------------------- 1 | var data2xml = require('../data2xml').data2xml; 2 | 3 | var data = { 4 | _attr : { 5 | xmlns : 'https://route53.amazonaws.com/doc/2011-05-05/', 6 | random : 'Quick test for \' and \"', 7 | }, 8 | ChangeBatch : { 9 | Comment : 'This is a comment (with dodgy characters like < & > \' and ")', 10 | Changes : { 11 | Change : [ 12 | { 13 | Action : 'CREATE', 14 | ResourceRecordSet : { 15 | Name : 'www.example.com', 16 | Type : 'A', 17 | TTL : 300, 18 | ResourceRecords : { 19 | ResourceRecord : [ 20 | { 21 | Value : '192.0.2.1' 22 | } 23 | ] 24 | } 25 | }, 26 | }, 27 | { 28 | Action : 'DELETE', 29 | ResourceRecordSet : { 30 | Name : 'foo.example.com', 31 | Type : 'A', 32 | TTL : 600, 33 | ResourceRecords : { 34 | ResourceRecord : [ 35 | { 36 | Value : '192.0.2.3' 37 | } 38 | ] 39 | } 40 | }, 41 | }, 42 | { 43 | Action : 'CREATE', 44 | ResourceRecordSet : { 45 | Name : 'foo.example.com', 46 | Type : 'A', 47 | TTL : 600, 48 | ResourceRecords : { 49 | ResourceRecord : [ 50 | { 51 | Value : '192.0.2.1' 52 | } 53 | ] 54 | } 55 | }, 56 | }, 57 | ], 58 | }, 59 | }, 60 | }; 61 | 62 | console.log(data2xml('ChangeResourceRecordSetsRequest', data)); 63 | console.log(); 64 | 65 | console.log( 66 | data2xml('TopLevelElement', { 67 | MyArray : [ 68 | 'Simple Value', 69 | { 70 | _attr : { type : 'colour' }, 71 | _value : 'White', 72 | } 73 | ], 74 | }) 75 | ); 76 | console.log(); 77 | -------------------------------------------------------------------------------- /examples/simple.js: -------------------------------------------------------------------------------- 1 | var data2xml = require('../data2xml').data2xml; 2 | 3 | var empty = {}; 4 | console.log(data2xml('TopLevel', empty)); 5 | console.log(); 6 | 7 | var simple = { 8 | Simple1 : 1, 9 | Simple2 : 2, 10 | }; 11 | console.log(data2xml('TopLevel', simple)); 12 | console.log(); 13 | 14 | var hierarchy = { 15 | Simple1 : 1, 16 | Simple2 : { 17 | Item1 : 'item 1', 18 | Item2 : 'item 2', 19 | Item3 : 'item 3', 20 | }, 21 | }; 22 | console.log(data2xml('TopLevel', hierarchy)); 23 | console.log(); 24 | 25 | var withAttrs = { 26 | mine : { 27 | _attr : { 28 | color : 'white', 29 | wheels : 4, 30 | }, 31 | _value : 'Ford Capri', 32 | }, 33 | yours : 'Vauxhall Astra', 34 | }; 35 | console.log(data2xml('cars', withAttrs)); 36 | console.log(); 37 | 38 | var withArray = { 39 | MyArray : [ 40 | 'Simple Value', 41 | { 42 | _attr : { type : 'colour' }, 43 | _value : 'White', 44 | } 45 | ], 46 | }; 47 | console.log(data2xml('TopLevel', withArray)); 48 | console.log(); 49 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "data2xml", 3 | "version": "1.3.4", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "balanced-match": { 8 | "version": "1.0.0", 9 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", 10 | "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", 11 | "dev": true 12 | }, 13 | "brace-expansion": { 14 | "version": "1.1.11", 15 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", 16 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", 17 | "dev": true, 18 | "requires": { 19 | "balanced-match": "^1.0.0", 20 | "concat-map": "0.0.1" 21 | } 22 | }, 23 | "concat-map": { 24 | "version": "0.0.1", 25 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", 26 | "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", 27 | "dev": true 28 | }, 29 | "deep-equal": { 30 | "version": "1.0.1", 31 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", 32 | "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", 33 | "dev": true 34 | }, 35 | "define-properties": { 36 | "version": "1.1.3", 37 | "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.3.tgz", 38 | "integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==", 39 | "dev": true, 40 | "requires": { 41 | "object-keys": "^1.0.12" 42 | } 43 | }, 44 | "defined": { 45 | "version": "1.0.0", 46 | "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz", 47 | "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=", 48 | "dev": true 49 | }, 50 | "es-abstract": { 51 | "version": "1.12.0", 52 | "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.12.0.tgz", 53 | "integrity": "sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==", 54 | "dev": true, 55 | "requires": { 56 | "es-to-primitive": "^1.1.1", 57 | "function-bind": "^1.1.1", 58 | "has": "^1.0.1", 59 | "is-callable": "^1.1.3", 60 | "is-regex": "^1.0.4" 61 | } 62 | }, 63 | "es-to-primitive": { 64 | "version": "1.2.0", 65 | "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.2.0.tgz", 66 | "integrity": "sha512-qZryBOJjV//LaxLTV6UC//WewneB3LcXOL9NP++ozKVXsIIIpm/2c13UDiD9Jp2eThsecw9m3jPqDwTyobcdbg==", 67 | "dev": true, 68 | "requires": { 69 | "is-callable": "^1.1.4", 70 | "is-date-object": "^1.0.1", 71 | "is-symbol": "^1.0.2" 72 | } 73 | }, 74 | "for-each": { 75 | "version": "0.3.3", 76 | "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", 77 | "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", 78 | "dev": true, 79 | "requires": { 80 | "is-callable": "^1.1.3" 81 | } 82 | }, 83 | "fs.realpath": { 84 | "version": "1.0.0", 85 | "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", 86 | "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", 87 | "dev": true 88 | }, 89 | "function-bind": { 90 | "version": "1.1.1", 91 | "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", 92 | "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", 93 | "dev": true 94 | }, 95 | "glob": { 96 | "version": "7.1.3", 97 | "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", 98 | "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", 99 | "dev": true, 100 | "requires": { 101 | "fs.realpath": "^1.0.0", 102 | "inflight": "^1.0.4", 103 | "inherits": "2", 104 | "minimatch": "^3.0.4", 105 | "once": "^1.3.0", 106 | "path-is-absolute": "^1.0.0" 107 | } 108 | }, 109 | "has": { 110 | "version": "1.0.3", 111 | "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", 112 | "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", 113 | "dev": true, 114 | "requires": { 115 | "function-bind": "^1.1.1" 116 | } 117 | }, 118 | "has-symbols": { 119 | "version": "1.0.0", 120 | "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.0.tgz", 121 | "integrity": "sha1-uhqPGvKg/DllD1yFA2dwQSIGO0Q=", 122 | "dev": true 123 | }, 124 | "inflight": { 125 | "version": "1.0.6", 126 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", 127 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", 128 | "dev": true, 129 | "requires": { 130 | "once": "^1.3.0", 131 | "wrappy": "1" 132 | } 133 | }, 134 | "inherits": { 135 | "version": "2.0.3", 136 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 137 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", 138 | "dev": true 139 | }, 140 | "is-callable": { 141 | "version": "1.1.4", 142 | "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.4.tgz", 143 | "integrity": "sha512-r5p9sxJjYnArLjObpjA4xu5EKI3CuKHkJXMhT7kwbpUyIFD1n5PMAsoPvWnvtZiNz7LjkYDRZhd7FlI0eMijEA==", 144 | "dev": true 145 | }, 146 | "is-date-object": { 147 | "version": "1.0.1", 148 | "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", 149 | "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", 150 | "dev": true 151 | }, 152 | "is-regex": { 153 | "version": "1.0.4", 154 | "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", 155 | "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", 156 | "dev": true, 157 | "requires": { 158 | "has": "^1.0.1" 159 | } 160 | }, 161 | "is-symbol": { 162 | "version": "1.0.2", 163 | "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.2.tgz", 164 | "integrity": "sha512-HS8bZ9ox60yCJLH9snBpIwv9pYUAkcuLhSA1oero1UB5y9aiQpRA8y2ex945AOtCZL1lJDeIk3G5LthswI46Lw==", 165 | "dev": true, 166 | "requires": { 167 | "has-symbols": "^1.0.0" 168 | } 169 | }, 170 | "minimatch": { 171 | "version": "3.0.4", 172 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", 173 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", 174 | "dev": true, 175 | "requires": { 176 | "brace-expansion": "^1.1.7" 177 | } 178 | }, 179 | "minimist": { 180 | "version": "1.2.6", 181 | "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.6.tgz", 182 | "integrity": "sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==", 183 | "dev": true 184 | }, 185 | "object-inspect": { 186 | "version": "1.6.0", 187 | "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.6.0.tgz", 188 | "integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==", 189 | "dev": true 190 | }, 191 | "object-keys": { 192 | "version": "1.0.12", 193 | "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", 194 | "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==", 195 | "dev": true 196 | }, 197 | "once": { 198 | "version": "1.4.0", 199 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", 200 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", 201 | "dev": true, 202 | "requires": { 203 | "wrappy": "1" 204 | } 205 | }, 206 | "path-is-absolute": { 207 | "version": "1.0.1", 208 | "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", 209 | "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", 210 | "dev": true 211 | }, 212 | "path-parse": { 213 | "version": "1.0.7", 214 | "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", 215 | "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", 216 | "dev": true 217 | }, 218 | "resolve": { 219 | "version": "1.7.1", 220 | "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz", 221 | "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", 222 | "dev": true, 223 | "requires": { 224 | "path-parse": "^1.0.5" 225 | } 226 | }, 227 | "resumer": { 228 | "version": "0.0.0", 229 | "resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz", 230 | "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", 231 | "dev": true, 232 | "requires": { 233 | "through": "~2.3.4" 234 | } 235 | }, 236 | "string.prototype.trim": { 237 | "version": "1.1.2", 238 | "resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.1.2.tgz", 239 | "integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=", 240 | "dev": true, 241 | "requires": { 242 | "define-properties": "^1.1.2", 243 | "es-abstract": "^1.5.0", 244 | "function-bind": "^1.0.2" 245 | } 246 | }, 247 | "tape": { 248 | "version": "4.9.1", 249 | "resolved": "https://registry.npmjs.org/tape/-/tape-4.9.1.tgz", 250 | "integrity": "sha512-6fKIXknLpoe/Jp4rzHKFPpJUHDHDqn8jus99IfPnHIjyz78HYlefTGD3b5EkbQzuLfaEvmfPK3IolLgq2xT3kw==", 251 | "dev": true, 252 | "requires": { 253 | "deep-equal": "~1.0.1", 254 | "defined": "~1.0.0", 255 | "for-each": "~0.3.3", 256 | "function-bind": "~1.1.1", 257 | "glob": "~7.1.2", 258 | "has": "~1.0.3", 259 | "inherits": "~2.0.3", 260 | "minimist": "~1.2.0", 261 | "object-inspect": "~1.6.0", 262 | "resolve": "~1.7.1", 263 | "resumer": "~0.0.0", 264 | "string.prototype.trim": "~1.1.2", 265 | "through": "~2.3.8" 266 | } 267 | }, 268 | "through": { 269 | "version": "2.3.8", 270 | "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", 271 | "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", 272 | "dev": true 273 | }, 274 | "wrappy": { 275 | "version": "1.0.2", 276 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", 277 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", 278 | "dev": true 279 | } 280 | } 281 | } 282 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "data2xml", 3 | "description": "A data to XML converter with a nice interface (for NodeJS).", 4 | "version": "1.3.4", 5 | "main": "data2xml.js", 6 | "homepage": "https://github.com/chilts/data2xml", 7 | "repository": { 8 | "type": "git", 9 | "url": "git@github.com:chilts/data2xml.git" 10 | }, 11 | "scripts": { 12 | "test": "set -e; for FILE in test/*.js; do node $FILE; done" 13 | }, 14 | "devDependencies": { 15 | "tape": "^4.9.1" 16 | }, 17 | "author": { 18 | "name": "Andrew Chilton", 19 | "email": "andychilton@gmail.com", 20 | "url": "https://chilts.org/" 21 | }, 22 | "keywords": [ 23 | "data", 24 | "xml", 25 | "data2xml", 26 | "datatoxml", 27 | "js2xml", 28 | "jstoxml", 29 | "json2xml", 30 | "jsontoxml" 31 | ], 32 | "license": "MIT" 33 | } 34 | -------------------------------------------------------------------------------- /test/basics.js: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // basics.js - tests for node-data2xml 4 | // 5 | // Copyright (c) 2011 Andrew Chilton - http://chilts.org/ 6 | // Written by Andrew Chilton 7 | // 8 | // License: http://opensource.org/licenses/MIT 9 | // 10 | // -------------------------------------------------------------------------------------------------------------------- 11 | 12 | var test = require('tape'); 13 | var data2xml = require('../data2xml'); 14 | 15 | // -------------------------------------------------------------------------------------------------------------------- 16 | 17 | test('some simple entities', function (t) { 18 | var test1 = data2xml.entitify(''); 19 | var exp1 = '<hello>'; 20 | t.equal(test1, exp1, 'LT and GT entitified correctly'); 21 | 22 | var test2 = data2xml.entitify('\'&\"'); 23 | var exp2 = ''&"'; 24 | t.equal(test2, exp2, 'other entities'); 25 | 26 | t.end(); 27 | }); 28 | 29 | test('making some elements', function (t) { 30 | var test1 = data2xml.makeStartTag('tagme'); 31 | var exp1 = ''; 32 | t.equal(test1, exp1, 'simple start tag'); 33 | 34 | var test2 = data2xml.makeEndTag('tagme'); 35 | var exp2 = ''; 36 | t.equal(test2, exp2, 'simple end tag'); 37 | 38 | var test3 = data2xml.makeStartTag('tagme', { attr : 'value' }); 39 | var exp3 = ''; 40 | t.equal(test3, exp3, '1) complex start tag'); 41 | 42 | var test4 = data2xml.makeStartTag('tagme', { attr : '' }); 43 | var exp4 = ''; 44 | t.equal(test4, exp4, '2) complex start tag'); 45 | 46 | var test5 = data2xml.makeStartTag('tagme', { attr1 : '', attr2 : 'val2' }); 47 | var exp5 = ''; 48 | t.equal(test5, exp5, '3) complex start tag'); 49 | 50 | t.end(); 51 | }); 52 | 53 | // -------------------------------------------------------------------------------------------------------------------- 54 | -------------------------------------------------------------------------------- /test/config-2.js: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // config-2.js - tests for node-data2xml 4 | // 5 | // Copyright (c) 2011 Andrew Chilton - http://chilts.org/ 6 | // Written by Andrew Chilton 7 | // 8 | // License: http://opensource.org/licenses/MIT 9 | // 10 | // -------------------------------------------------------------------------------------------------------------------- 11 | 12 | var test = require('tape'); 13 | var data2xml = require('../data2xml'); 14 | 15 | var declaration = '\n'; 16 | 17 | // -------------------------------------------------------------------------------------------------------------------- 18 | 19 | var tests = [ 20 | { 21 | name : 'one element structure with an xmlns', 22 | element : 'topelement', 23 | data : { 24 | '_attr' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, 25 | second : 'value', 26 | 'undefined' : undefined, 27 | 'null' : null, 28 | }, 29 | exp1 : declaration + 'value', 30 | exp2 : declaration + 'value' 31 | }, 32 | { 33 | name : 'element with attributes and value undefined', 34 | element : 'topelement', 35 | data : { 36 | '_attr' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, 37 | second : 'value', 38 | 'undefined' : {"_attr": {"key1": "something", "key2": "else"}, "_value": undefined}, 39 | 'null' : null, 40 | }, 41 | exp1 : declaration + 'value', 42 | exp2 : declaration + 'value' 43 | }, 44 | { 45 | name : 'element with attributes and value null', 46 | element : 'topelement', 47 | data : { 48 | '_attr' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, 49 | second : 'value', 50 | 'undefined' : undefined, 51 | 'null' : {"_attr":{"key1":"value", "key2": "value2"}, "_value": null}, 52 | }, 53 | exp1 : declaration + 'value', 54 | exp2 : declaration + 'value' 55 | }, 56 | { 57 | name : 'complex 4 element array with some attributes', 58 | element : 'topelement', 59 | data : { item : [ 60 | { '_attr' : { type : 'a' }, '_value' : 'val1' }, 61 | { '_attr' : { type : 'b' }, '_value' : 'val2' }, 62 | 'val3', 63 | { '_value' : 'val4' }, 64 | ] }, 65 | exp1 : declaration + 'val1val2val3val4', 66 | exp2 : declaration + 'val1val2val3val4' 67 | }, 68 | ]; 69 | 70 | var convert1 = data2xml({ 'undefined' : 'empty', 'null' : 'closed' }); 71 | test('1) some simple xml with undefined or null values', function (t) { 72 | tests.forEach(function(test) { 73 | var xml = convert1(test.element, test.data, { attrProp : '@', valProp : '#' }); 74 | t.equal(xml, test.exp1, test.name); 75 | }); 76 | 77 | t.end(); 78 | }); 79 | 80 | var convert2 = data2xml({ 'undefined' : 'closed', 'null' : 'empty' }); 81 | test('2) some simple xml with undefined or null values', function (t) { 82 | tests.forEach(function(test) { 83 | var xml = convert2(test.element, test.data, { attrProp : '@', valProp : '#' }); 84 | t.equal(xml, test.exp2, test.name); 85 | }); 86 | 87 | t.end(); 88 | }); 89 | 90 | // -------------------------------------------------------------------------------------------------------------------- 91 | -------------------------------------------------------------------------------- /test/config.js: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // config.js - tests for node-data2xml 4 | // 5 | // Copyright (c) 2011 Andrew Chilton - http://chilts.org/ 6 | // Written by Andrew Chilton 7 | // 8 | // License: http://opensource.org/licenses/MIT 9 | // 10 | // -------------------------------------------------------------------------------------------------------------------- 11 | 12 | var test = require('tape'); 13 | var data2xml = require('../data2xml'); 14 | 15 | var declaration = '\n'; 16 | 17 | // -------------------------------------------------------------------------------------------------------------------- 18 | 19 | test('some simple xml with custom attributes, values and cdata', function (t) { 20 | var convert = data2xml({ attrProp : '@', valProp : '#', cdataProp : '%' }); 21 | 22 | var tests = [{ 23 | name : 'one element structure with an xmlns', 24 | element : 'topelement', 25 | data : { '@' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, second : 'value' }, 26 | exp : declaration + 'value' 27 | }, { 28 | name : 'complex 4 element array with some attributes', 29 | element : 'topelement', 30 | data : { item : [ 31 | { '@' : { type : 'a' }, '#' : 'val1' }, 32 | { '@' : { type : 'b' }, '#' : 'val2' }, 33 | 'val3', 34 | { '#' : 'val4' }, 35 | ] }, 36 | exp : declaration + 'val1val2val3val4' 37 | }, { 38 | name : 'simple element with cdata set', 39 | element : 'topelement', 40 | data : { '%' : 'Some text with unescaped HTML data.' }, 41 | exp : declaration + 'unescaped HTML data.]]>' 42 | }]; 43 | 44 | tests.forEach(function(test) { 45 | var xml = convert(test.element, test.data); 46 | t.equal(xml, test.exp, test.name); 47 | }); 48 | 49 | t.end(); 50 | }); 51 | 52 | test('it\'s possible to omit the XML declaration', function (t) { 53 | var convert = data2xml({xmlDecl: false}); 54 | 55 | t.equal( 56 | convert('moo', {foo: 'bar', baz: 42}), 57 | 'bar42', 58 | 'must not be declared as XML' 59 | ); 60 | 61 | t.end(); 62 | }); 63 | 64 | // -------------------------------------------------------------------------------------------------------------------- 65 | -------------------------------------------------------------------------------- /test/doc-xml.js: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // xml-generation.js - tests for node-data2xml 4 | // 5 | // Copyright (c) 2011 Andrew Chilton - http://chilts.org/ 6 | // Written by Andrew Chilton 7 | // 8 | // License: http://opensource.org/licenses/MIT 9 | // 10 | // -------------------------------------------------------------------------------------------------------------------- 11 | 12 | var test = require('tape'); 13 | var data2xml = require('../data2xml'); 14 | 15 | // -------------------------------------------------------------------------------------------------------------------- 16 | 17 | var tests = [ 18 | { 19 | name : 'document natured XML', 20 | declaration : '\n', 21 | element : 'name', 22 | data : { 23 | text: [ 24 | { 25 | _attr: { 26 | 'xml:lang': 'de-DE' 27 | }, 28 | _value: 'The german name' 29 | }, 30 | ], 31 | _value: 'My app name', 32 | }, 33 | exp : '\nMy app nameThe german name' 34 | }, 35 | { 36 | name : 'XML declared standalone', 37 | declaration : '\n', 38 | element : 'name', 39 | data : { 40 | text: [ 41 | { 42 | _attr: { 43 | 'xml:lang': 'de-DE' 44 | }, 45 | _value: 'The german name' 46 | }, 47 | ], 48 | _value: 'My app name', 49 | }, 50 | exp : '\nMy app nameThe german name' 51 | }, 52 | ]; 53 | 54 | test('some simple xml', function (t) { 55 | tests.forEach(function(test) { 56 | var convert = data2xml({xmlHeader: test.declaration}); 57 | var xml = convert(test.element, test.data); 58 | t.equal(xml, test.exp, test.name); 59 | }); 60 | 61 | t.end(); 62 | }); 63 | 64 | // -------------------------------------------------------------------------------------------------------------------- 65 | -------------------------------------------------------------------------------- /test/load.js: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // load.js - tests for node-data2xml 4 | // 5 | // Copyright (c) 2011 Andrew Chilton - http://chilts.org/ 6 | // Written by Andrew Chilton 7 | // 8 | // License: http://opensource.org/licenses/MIT 9 | // 10 | // -------------------------------------------------------------------------------------------------------------------- 11 | 12 | var test = require('tape'); 13 | var data2xml; 14 | 15 | // -------------------------------------------------------------------------------------------------------------------- 16 | 17 | test('load data2xml', function (t) { 18 | data2xml = require('../data2xml'); 19 | t.ok(data2xml, 'package loaded'); 20 | 21 | t.end(); 22 | }); 23 | 24 | // -------------------------------------------------------------------------------------------------------------------- 25 | -------------------------------------------------------------------------------- /test/xml-generation.js: -------------------------------------------------------------------------------- 1 | // -------------------------------------------------------------------------------------------------------------------- 2 | // 3 | // xml-generation.js - tests for node-data2xml 4 | // 5 | // Copyright (c) 2011 Andrew Chilton - http://chilts.org/ 6 | // Written by Andrew Chilton 7 | // 8 | // License: http://opensource.org/licenses/MIT 9 | // 10 | // -------------------------------------------------------------------------------------------------------------------- 11 | 12 | var test = require('tape'); 13 | var data2xml = require('../data2xml')({}); 14 | 15 | var declaration = '\n'; 16 | 17 | // -------------------------------------------------------------------------------------------------------------------- 18 | 19 | var tests = [ 20 | { 21 | name : 'empty structure', 22 | element : 'topelement', 23 | data : {}, 24 | exp : declaration + '' 25 | }, 26 | { 27 | name : 'one element structure', 28 | element : 'topelement', 29 | data : { second : 'value' }, 30 | exp : declaration + 'value' 31 | }, 32 | { 33 | name : 'one element structure which has an empty value', 34 | element : 'topelement', 35 | data : { second : '' }, 36 | exp : declaration + '' 37 | }, 38 | { 39 | name : 'one element structure which has a undefined value', 40 | element : 'topelement', 41 | data : { second : undefined }, 42 | exp : declaration + '' 43 | }, 44 | { 45 | name : 'one element structure which has a null value', 46 | element : 'topelement', 47 | data : { second : null }, 48 | exp : declaration + '' 49 | }, 50 | { 51 | name : 'one element structure with an xmlns', 52 | element : 'topelement', 53 | data : { _attr : { xmlns : 'http://www.appsattic.com/xml/namespace' }, second : 'value' }, 54 | exp : declaration + 'value' 55 | }, 56 | { 57 | name : 'two elements', 58 | element : 'topelement', 59 | data : { second : 'val2', third : 'val3' }, 60 | exp : declaration + 'val2val3' 61 | }, 62 | { 63 | name : 'simple hierarchical elements', 64 | element : 'topelement', 65 | data : { simple : 'val2', complex : { test : 'val4' } }, 66 | exp : declaration + 'val2val4' 67 | }, 68 | { 69 | name : 'simple one element array', 70 | element : 'topelement', 71 | data : { array : [ { item : 'value' } ] }, 72 | exp : declaration + 'value' 73 | }, 74 | { 75 | name : 'simple two element array #1', 76 | element : 'topelement', 77 | data : { array : [ { item : 'value1' }, 'value2' ] }, 78 | exp : declaration + 'value1value2' 79 | }, 80 | { 81 | name : 'simple two element array #2', 82 | element : 'topelement', 83 | data : { array : [ 'value1', 'value2' ] }, 84 | exp : declaration + 'value1value2' 85 | }, 86 | { 87 | name : 'simple two element array #3', 88 | element : 'topelement', 89 | data : { array : { item : [ 'value1', 'value2' ] } }, 90 | exp : declaration + 'value1value2' 91 | }, 92 | { 93 | name : 'complex 4 element array with some attributes', 94 | element : 'topelement', 95 | data : { item : [ 96 | { _attr : { type : 'a' }, _value : 'val1' }, 97 | { _attr : { type : 'b' }, _value : 'val2' }, 98 | 'val3', 99 | { _value : 'val4' }, 100 | ] }, 101 | exp : declaration + 'val1val2val3val4' 102 | }, 103 | { 104 | name : 'element with CDATA', 105 | element : 'name', 106 | data : { 107 | text: [ 108 | { 109 | _attr: { 110 | 'xml:lang': 'de-DE' 111 | }, 112 | _cdata: 'Some text with unescaped HTML data.' 113 | }, 114 | ], 115 | _value: 'My app name', 116 | }, 117 | exp : declaration + 'My app nameunescaped HTML data.]]>' 118 | }, 119 | { 120 | name : 'element with CDATA containing a ]]>', 121 | element : 'name', 122 | data : { 123 | text: [ 124 | { 125 | _attr: { 126 | 'xml:lang': 'de-DE' 127 | }, 128 | _cdata: 'Some text with ]]> twice ]]> inside it.' 129 | }, 130 | ], 131 | _value: 'My app name', 132 | }, 133 | exp : declaration + 'My app name twice ]]]]> inside it.]]>' 134 | }, 135 | ]; 136 | 137 | test('some simple xml', function (t) { 138 | tests.forEach(function(test) { 139 | var xml = data2xml(test.element, test.data); 140 | t.equal(xml, test.exp, test.name); 141 | }); 142 | 143 | t.end(); 144 | }); 145 | 146 | // -------------------------------------------------------------------------------------------------------------------- 147 | --------------------------------------------------------------------------------