json2xml

JavaScript

var json = [
    {
        "title": "Рабочая сеть",
        "description": "Сеть на работе",
        "categoryID": "Wi-Fi сети",
        "secret": {
            "network_name": "ROSTELECOM_2151",
            "password": "123456789"
        }
    },
    {
        "title": "jsfiddle.net",
        "description": "Песочница",
        "categoryID": "Web-сайты",
        "secret": {
            "url": "https://jsfiddle.net/",
            "login": "user",
            "password": "password"
        }
    },
    {
        "title": "Домашняя сеть",
        "description": "Мой домашний Wi-Fi",
        "categoryID": "Wi-Fi сети",
        "secret": {
            "network_name": "ROSTELECOM_BEDE",
            "password": "RCAQGRRC"
        }
    }
];

(function(root, factory) {
	if (typeof exports === "object") {
		// CommonJS
		module.exports = exports = factory();
	} else if (typeof define === "function" && define.amd) {
		// AMD
		define([], factory);
	} else {
		// Global (browser)
		root.json2xml = factory();
	}
}(this || window, function() {
	'use strict';
	/**
	 * Module description
	 *
	 * @name json2xml
	 */
	var json2xml = (function() {
		var self = this;

		var makeSafe = function(str) {
			// xml special charaters
			str = str.replace(/</g, '&lt;').replace(/&/g, '&amp;');
			return lines(str);
		}

		var lines = function(str) {
			// normalise line endings, all in file will be unixy
			str = str.replace(/\r\n/g, '\n');
			return str;
		};

		self.convert = function(o) {
			var toXml = function(v, name, ind) {
							var xml = "";
							if (v instanceof Array) {
								for (var i = 0, n = v.length; i < n; i++) {
									xml += toXml(v[i], name, ind + "");
								}
							} else if (typeof(v) == "object") {
								var hasChild = false;
								xml += ind + "<" + name;
								for (var m in v) {
									if (m.charAt(0) == "@") {
										xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";
									} else {
										hasChild =...