一、保存
function exportToXml(jsonStr){ var obj = eval(‘(‘+ jsonStr + ‘)‘); var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); //创建两条处理指令 var newPI=xmlDoc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\""); xmlDoc.appendChild(newPI); //创建根元素 var documentElement=xmlDoc.createElement("root"); xmlDoc.appendChild(documentElement); //创建CDATA //var newCD=xmlDoc.createCDATASection("This is a CDATASection node"); //xmlDoc.documentElement.appendChild(newCD); var row=xmlDoc.createElement("row"); var task=xmlDoc.createElement("Task"); task.setAttribute("TaskID",obj.AJGUID); var aj_dj_guid=xmlDoc.createElement("AJ_DJ_GUID"); aj_dj_guid.setAttribute("name", "案件编号"); var taskname=xmlDoc.createElement("TaskName"); taskname.setAttribute("name", "任务名称"); var aj_mc=xmlDoc.createElement("AJ_MC"); aj_mc.setAttribute("name", "案件名称"); var aj_dd=xmlDoc.createElement("AJ_DD"); aj_dd.setAttribute("name", "案件地点"); var sfhcwc=xmlDoc.createElement("SFHCWC"); sfhcwc.setAttribute("name", "是否核查完成"); var hcsj=xmlDoc.createElement("HCSJ"); hcsj.setAttribute("name", "核查时间"); var refpointx=xmlDoc.createElement("RefPointX"); refpointx.setAttribute("name", "巡查地点X坐标"); var refpointy=xmlDoc.createElement("RefPointY"); refpointy.setAttribute("name", "巡查地点Y坐标"); var taskdesc=xmlDoc.createElement("TaskDesc"); taskdesc.setAttribute("name", "任务说明"); //添加子元素 task.appendChild(aj_dj_guid); task.appendChild(taskname); task.appendChild(aj_mc); task.appendChild(aj_dd); task.appendChild(sfhcwc); task.appendChild(hcsj); task.appendChild(refpointx); task.appendChild(refpointy); task.appendChild(taskdesc); //创建text节点,作为元素的内容 var aj_dj_guidtext=xmlDoc.createTextNode(‘‘); var tasknametext=xmlDoc.createTextNode(‘‘); var aj_mctext=xmlDoc.createTextNode(‘‘); var aj_ddtext=xmlDoc.createTextNode(‘‘); var sfhcwctext=xmlDoc.createTextNode(‘‘); var hcsjtext=xmlDoc.createTextNode(‘‘); var refpointxtext=xmlDoc.createTextNode(‘‘); var refpointytext=xmlDoc.createTextNode(‘‘); var taskdesctext=xmlDoc.createTextNode(‘‘); //为元素赋值 aj_dj_guid.appendChild(aj_dj_guidtext); taskname.appendChild(tasknametext); aj_mc.appendChild(aj_mctext); aj_dd.appendChild(aj_ddtext); sfhcwc.appendChild(sfhcwctext); hcsj.appendChild(hcsjtext); refpointx.appendChild(refpointxtext); refpointy.appendChild(refpointytext); taskdesc.appendChild(taskdesctext); row.appendChild(task);//添加到row元素中 var project=xmlDoc.createElement("Project"); var aj_dj_guid_project=xmlDoc.createElement("AJ_DJ_GUID"); var xzqdm=xmlDoc.createElement("XZQDM"); var ajmc=xmlDoc.createElement("AJMC"); var bz=xmlDoc.createElement("BZ"); //添加子元素 project.appendChild(aj_dj_guid_project); project.appendChild(xzqdm); project.appendChild(ajmc); project.appendChild(bz); //创建text节点,作为元素的内容 var aj_dj_guid_projecttext=xmlDoc.createTextNode(obj.AJGUID); var xzqdmtext=xmlDoc.createTextNode(‘‘); var ajmctext=xmlDoc.createTextNode(obj.DKMC); var bztext=xmlDoc.createTextNode(‘‘); //为元素赋值 aj_dj_guid_project.appendChild(aj_dj_guid_projecttext); xzqdm.appendChild(xzqdmtext); ajmc.appendChild(ajmctext); bz.appendChild(bztext); var ajdks=xmlDoc.createElement("AJDKS"); ajdks.setAttribute("count", "1"); var ajdk=xmlDoc.createElement("AJDK"); var attribute=xmlDoc.createElement("Attribute"); var dkbh=xmlDoc.createElement("DKBH"); var dkmc=xmlDoc.createElement("DKMC"); var tdyt=xmlDoc.createElement("TDYT"); var dkmj=xmlDoc.createElement("DKMJ"); //添加子元素 attribute.appendChild(dkbh); attribute.appendChild(dkmc); attribute.appendChild(tdyt); attribute.appendChild(dkmj); //创建text节点,作为元素的内容 var dkbhtext=xmlDoc.createTextNode(‘‘); var dkmctext=xmlDoc.createTextNode(obj.DKMC); var tdyttext=xmlDoc.createTextNode(‘‘); var dkmjtext=xmlDoc.createTextNode(‘‘); //为元素赋值 dkbh.appendChild(dkbhtext); dkmc.appendChild(dkmctext); tdyt.appendChild(tdyttext); dkmj.appendChild(dkmjtext); ajdk.appendChild(attribute);//添加到ajdk元素中 var shape=xmlDoc.createElement("Shape"); for(i=0;i<obj.PointNumber;i++){ var point=xmlDoc.createElement("Point"); point.setAttribute("NO", ""+(i+1)+""); point.setAttribute("X", ""+obj.SHAPE[i].X+""); point.setAttribute("Y", ""+obj.SHAPE[i].Y+""); //添加子元素 shape.appendChild(point); } ajdk.appendChild(shape);//添加到ajdk元素中 var appendix=xmlDoc.createElement("Appendix"); ajdk.appendChild(appendix);//添加到ajdk元素中 var appendixtext=xmlDoc.createTextNode(‘‘) appendix.appendChild(appendixtext); ajdks.appendChild(ajdk);//添加到ajdks元素中 project.appendChild(ajdks);//添加到project元素中 row.appendChild(project);//添加到row元素中 xmlDoc.documentElement.appendChild(row); //alert(xmlDoc.xml); xmlDoc.save("D:\\"+obj.DKMC+".xml"); alert("保存到【D:\\"+obj.DKMC+".xml】成功!"); }
二、读取
function importXmlCoord(filePath){ try{ if(window.ActiveXObject) { var xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false" xmlDoc.load(filePath); var points = xmlDoc.documentElement.getElementsByTagName("Point"); var coordStr = "1,"+points.length+",";//只有一个圈(不含多圈地) for(i=0;i<points.length;i++){ var no=points[i].getAttribute("NO"); var x=points[i].getAttribute("X"); var y=points[i].getAttribute("Y"); var tempI = y.indexOf("."); var tempY = ""; if (tempI > 0){ tempY = y.substring(0,tempI); //y坐标整数位 }else{ tempY = yCoords; } if(isDegreeZone == "0"){ if (tempY.length == 8){ //y坐标整数位为8,则去掉带号2位 tempY = yCoords.substring(2,y.length); }else{ tempY = y; } }else{ tempY = y; } coordStr += no+","+x+","+y+","; } coordStr += "0,0,0,1,"; return coordStr; } }catch(e){ alert("导入XML格式坐标失败,请检查格式是否正确。"); } }
时间: 2024-10-04 07:21:47