//***************获取Table中的数据,并提供Xml内容的支持类*********** //获取指定id对应的Table对象 function TableXmlEngen(oTab) { var oTable; if (!oTab) { } else { oTable = oTab; } this.Init = function(oTab) { oTable = oTab; }; //返回属性满足要求的所有行的数组对象 this.getRows = function(oTable, sJuageAttr, sFitValue) { var nIndex = 0; var oRows = new Array(); for (nIndex = 0; nIndex < oTable.rows.length; nIndex++) { if (oTable.rows[nIndex].getAttribute(sJuageAttr) == sFitValue) { //add to array oRows.push(oTable.rows[nIndex]); } else if (!sJuageAttr || !sFitValue) { //如果第二、三任何一个数据不存在,则表示不过滤 oRows.push(oTable.rows[nIndex]); } } return oRows; }; //返回一行的所有的列的属性值 //依赖于:getUseAttributes this.getRowProperty = function(oRow) { var oPropertys = []; var nIndex = 0; for (nIndex = 0; nIndex < oRow.cells.length; nIndex++) { oPropertys.push(getUseAttributes(oRow.cells[nIndex])); } return oPropertys; }; //返回的是键值对的集合,name:value; //函数返回所有的有意义的属性和属性值,默认的空白属性和属性值都不会返回 this.getUseAttributes = function(oUnit) { var nIndex = 0; var oAttrs = []; var oTmpData = {}; for (nIndex = 0; nIndex < oUnit.attributes.length; nIndex++) { var oAttribute = oUnit.attributes[nIndex]; if (oAttribute.value != "null" && !oAttribute.value == false) { oTmpData = { name: oAttribute.name, value: oAttribute.value }; oAttrs.push(oTmpData); } } return oAttrs; }; this.getRowData = function(oRow) { var oRowData = []; var nIndex = 0; for (nIndex = 0; nIndex < oRow.cells.length; nIndex++) { if (!oRow.cells[nIndex].getAttribute("name")) { var oDic = { name: "name" + nIndex, value: oRow.cells[nIndex].innerHTML }; oRowData.push(oDic); } else { var oDic = { name: oRow.cells[nIndex].getAttribute("name"), value: oRow.cells[nIndex].innerHTML }; oRowData.push(oDic); } } oRowData.appendData = function(oXml, oTopNode) { var oNewNode = oXml.createElement("data"); var nIndex = 0; oTopNode.appendChild(oNewNode); for (nIndex = 0; nIndex < oRowData.length; nIndex++) { var oNode = oXml.createElement("data-collum"); var oNodeText = oXml.createTextNode(oRowData[nIndex].value); oNode.setAttribute("name", oRowData[nIndex].name); oNode.appendChild(oNodeText); oNewNode.appendChild(oNode); } return; }; return oRowData; }; this.getTableData = function(sRowType, sTypeValue) { var nIndex = 0; var oTableInfo = []; var oRows = getRows(oTable, sRowType, sTypeValue); for (nIndex = 0; nIndex < oRows.length; nIndex++) { oTableInfo.push(getRowData(oRows[nIndex])); } oTableInfo.createXmlObj = function() { var sXml = ""; var nIndex = 0; var oXml = new ActiveXObject("Microsoft.XMLDOM"); //先创建好一个空的Xml对象 var oTopNode = oXml.createElement("SunSoft_Data"); //创建一个顶级节点 oXml.appendChild(oTopNode); //将这个顶级节点加到Xml对象上 //oTopNode是顶级Node(节点) for (nIndex = 0; nIndex < oTableInfo.length; nIndex++) { oTableInfo[nIndex].appendData(oXml, oTopNode); //为顶级节点增加子节点,每个子节点代表一行的数据,调用的是oRowData.appendData方法 } return oXml; } return oTableInfo; }; }