Author: ygrego Date: 2015-05-12 08:30:28 +0000 (Tue, 12 May 2015) New Revision: 1335 Url: http://forge.nuiton.org/projects/sandbox/repository/revisions/1335 Log: Implementation of a xml parser to manage a xml document. Added: oipf/js/utils/XmlParser.js Added: oipf/js/utils/XmlParser.js =================================================================== --- oipf/js/utils/XmlParser.js (rev 0) +++ oipf/js/utils/XmlParser.js 2015-05-12 08:30:28 UTC (rev 1335) @@ -0,0 +1,73 @@ +/* + * 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 XmlParser = Class.extend({ + + init: function() { + + this.initParser(); + this.xmlDocument; + }, + + initParser: function() { + if (window.DOMParser) { + this.parser=new DOMParser(); + } + }, + + createAXmlStringDocument: function(defaultPropertiesObject) { + console.log("Method createAXmlStringDocument called"); + var defaultProperties = defaultPropertiesObject.default; + var objectsNames = Object.keys(defaultProperties); + var xmlStringDocument = "<" + defaultPropertiesObject.roots + ">"; + for (var i = 0, l = objectsNames.length; i < l; i++) { + + var currentObjectName = objectsNames[i]; + xmlStringDocument += "<" + currentObjectName; + var currentObjectData = defaultProperties[currentObjectName]; + var currentAttributes = currentObjectData.attributes; + + if (currentAttributes) { + xmlStringDocument = this.createAttributes(currentAttributes, + xmlStringDocument); + } + + var currentObjectValue = currentObjectData.value; + xmlStringDocument += ">" + currentObjectValue; + xmlStringDocument += "</" + currentObjectName + ">"; + } + xmlStringDocument += "</" + defaultPropertiesObject.roots + ">"; + console.log("Method createAXmlStringDocument completed"); + return xmlStringDocument; + }, + + createAttributes: function(currentAttributes, xmlStringDocument) { + var currentAttributesNames = Object.keys(currentAttributes); + for (var i = 0, l = currentAttributesNames.length; i < l; i++) { + var currentAttributeName = currentAttributesNames[i]; + var currentAttributeValue = currentAttributes[currentAttributeName]; + xmlStringDocument += " " + currentAttributeName + + "='" + currentAttributeValue + "'"; + } + return xmlStringDocument; + }, + + getXmlDocument: function(defaultProperties) { + if (!this.parser) { + this.initParser(); + } + var xmlStringDocument = + this.createAXmlStringDocument(defaultProperties); + + this.xmlDocument = + this.parser.parseFromString(xmlStringDocument, "text/xml"); + + return this.xmlDocument; + } + +}); + +