Author: ygrego Date: 2015-05-28 07:34:18 +0000 (Thu, 28 May 2015) New Revision: 1506 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1506 Log: Addition of a new class "XmlToJson". Added: oipf/js/utils/XmlToJson.js Added: oipf/js/utils/XmlToJson.js =================================================================== --- oipf/js/utils/XmlToJson.js (rev 0) +++ oipf/js/utils/XmlToJson.js 2015-05-28 07:34:18 UTC (rev 1506) @@ -0,0 +1,173 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +var XmlToJson = Class.extend({ + unwantedNodeName: { + "#text": true, + "#comment": true + }, + + mappingValue: { + "true": true, + "false": false + }, + + wantedNode: "channel", + + init: function(xmlDocument) { + xmlDocument && (this.xmlDocument = xmlDocument) && + (this.json = {}) && this.initJson(); + }, + + initJson: function() { + if (!this.xmlDocument) { + return null; + } + console.log(this.xmlDocument.firstChild); + this.rootsNode = this.xmlDocument.firstChild; + + //We already know that document contains at least one child. + var rootsName = this.rootsNode.nodeName; + this.rootsElementJson = this.json[rootsName] = {}; + + var rootsObject = this.json[rootsName]; + + this.convertAttributesToProperties(rootsObject, this.rootsNode); + }, + + convertAttributesToProperties: function(object, node) { + var attributesList = node.attributes; + + for (var i = 0, li = attributesList.length; i < li; i++) { + var currentAttributes = attributesList[i]; + var name = currentAttributes.name; + var value = currentAttributes.value; + object[name] = value; + } + return object; + }, + + getJson: function() { + if (!this.json) { + console.log("Your xml document is not valid or null."); + return null; + } + var data = this.rootsNode.childNodes; + var dataStoredByKind = {}; + + for (var i = 0, li = data.length; i < li; i++) { + var currentNode = data[i]; + this.setPropertiesFromDomNodeList(currentNode, this.rootsElementJson); + } + return this.json; + }, + + setPropertiesFromDomNodeList: function(currentNode, parentContainer) { + + if (this.unwantedNodeName[currentNode.nodeName]) { + console.log("This node are useless.", currentNode.nodeName); + return; + } + console.log(currentNode.nodeName); + var childNodes = currentNode.childNodes; + var childNodeName; + var currentChildNode; + var nodeDataMap = {}; + if (currentNode.hasAttributes()) { + nodeDataMap = + this.convertAttributesToProperties(nodeDataMap, currentNode); + } + + for (var i = 0, li = childNodes.length; i < li; i++) { + currentChildNode = childNodes[i]; + childNodeName = currentChildNode.nodeName; + console.log(childNodeName); + + if (!this.unwantedNodeName[childNodeName]) { + nodeDataMap = this.setPropertiesFromDomNodeList + (currentChildNode, nodeDataMap); + } + + if (/\w+/.exec(currentChildNode.nodeValue) && + (currentChildNode.childNodes.length == 0)) { + + console.log(currentChildNode.nodeValue); + + if (!this.isMapNotEmpty(nodeDataMap)) { + var currentNodeName = currentNode.nodeName; + var key = currentNodeName; + var value = currentChildNode.nodeValue; + this.addPropertiesToMap(parentContainer, key, value, + currentNode.parentNode); + } + + //This add concern only the nodes which have an atomic content + if (this.isMapNotEmpty(nodeDataMap) && !currentChildNode) { + var key = "#text"; + var value = currentChildNode.nodeValue; + this.addPropertiesToMap(nodeDataMap, key, value, + currentNode.parentNode); + } + } + } + + //Addition of node in parent container even if this one have not a value + if (!this.unwantedNodeName[currentNode.nodeName] && + !currentChildNode) { + + var currentNodeName = currentNode.nodeName; + var key = currentNodeName; + this.addPropertiesToMap(parentContainer, key, null, + currentNode.parentNode); + } + + var currentNodeName = currentNode.nodeName; + if (this.isMapNotEmpty(nodeDataMap)) { + var key = currentNodeName; +// parentContainer[key] = nodeDataMap; + this.addPropertiesToMap(parentContainer, key, nodeDataMap, + currentNode.parentNode); + } + + return parentContainer; + }, + + isMapNotEmpty: function(map) { + return Object.keys(map).length > 0; + }, + + /* + * + */ + addPropertiesToMap: function(map, key, value, parentNode) { +// map[key] = value; + + var childNodesWithSpecificName = parentNode.getElementsByTagName(key); + var specificNodesNumber = childNodesWithSpecificName.length; + + if ((specificNodesNumber > 1) && !map[key]) { + map[key] = []; + } + + var nodeValue = map[key]; + + var value = this.setValue(value); + + if (nodeValue && (nodeValue instanceof Array)) { + map[key].push(value); + } else { + map[key] = value; + } + }, + + setValue: function(value) { + var newValue = this.mappingValue[value]; + if ((typeof value) == "string" && newValue != undefined) { + return newValue; + } + return value; + } +});
participants (1)
-
ygregoï¼ users.nuiton.org