/** * * @param {Object} xmlText * @returns {XmlDataSourceTool} */ var XmlDataSourceTool = function (xmlText) { this.xmlDoc = null; this.oneColumn = ""; /** * * @param {String} xmlText * @returns {void} */ this.init = function (xmlText) { if (xmlText === undefined) { return null; } if (xmlText === null) { return; } var xmlTextType = this.toType(xmlText); if (xmlTextType === "xmldocument" || xmlTextType === "document") { this.xmlDoc = xmlText; } else { var parser = null; if (window.DOMParser) { parser = new DOMParser(); this.xmlDoc = parser.parseFromString(xmlText, "text/xml"); } else // Internet Explorer { this.xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); this.xmlDoc.async = false; this.xmlDoc.loadXML(xmlText); } } try { var x = this.xmlDoc.documentElement; var firstNode = this.getFirstChild(x); for (var i = 0; i < firstNode.childNodes.length; i++) { if (firstNode.childNodes[i].nodeType === 1) { this.oneColumn = firstNode.childNodes[i].nodeName; break; } } } catch (err) { this.oneColumn = ""; } }; this.importDataWindow = function (dwobj) { }; this.setXmlDoc = function (xmlDoc) { this.xmlDoc = xmlDoc; var x = this.xmlDoc.documentElement; var firstNode = this.getFirstChild(x); for (var i = 0; i < firstNode.childNodes.length; i++) { if (firstNode.childNodes[i].nodeType === 1) { this.oneColumn = firstNode.childNodes[i].nodeName; break; } } }; /** * * @param {Element} n * @returns {Element} */ this.getFirstChild = function (n) { var x = n.firstChild; while (x.nodeType !== 1) { x = x.nextSibling; } return x; }; /** * * @param {Number} row * @param {String} column * @returns {String} */ this.GetItemString = function (row, column) { var result = ""; try { result = (this.xmlDoc.getElementsByTagName(column)[row].childNodes[0].nodeValue) + ""; if (result === null || result === undefined) { result = ""; } } catch (err) { result = ""; } return result; }; /** * * @param {type} row * @param {type} column * @returns {String} */ this.GetItemStringTrim = function (row, column) { var result = this.GetItemString(row, column).replace(/^\s+|\s+$/g, ''); return result; }; /** * * @param {Number} row * @param {String} column * @returns {Number} */ this.GetItemNumber = function (row, column) { var text = this.GetItemString(row, column); var result = 0; try { if (text.indexOf(".") > 0) { result = parseFloat(text); } else { result = parseInt(text); } } catch (err) { result = 0; } return result; }; /** * * @returns {Number} */ this.RowCount = function () { //xhr.responseXML.getElementsByTagName( "pet" ).length if (this.oneColumn == "") { return -1; } return this.xmlDoc.getElementsByTagName(this.oneColumn).length; }; /** * * @returns {String} */ this.toString = function () { var xmlString = ""; //IE if (window.ActiveXObject) { xmlString = this.xmlDoc.xml + ""; } // code for Mozilla, Firefox, Opera, etc. else { xmlString = (new XMLSerializer()).serializeToString(this.xmlDoc) + ""; } return xmlString; }; /** * * @param {type} obj * @returns {String} */ this.toType = function (obj) { return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase(); }; this.init(xmlText); };