js 对XML文件的保存与读取
一、保存(在前端用js创建xml格式)
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】成功!"); }
二、保存(在后台组成xml格式字符串传到前端)
1、后台flex代码
private function expertCMJFYWXML():void{ var ajmc:String = this.ajmctxt.text; if(ajmc == "请先填写案件名称"){ ajmc = "临时案件"; } var pointNunber:int = 0; var points:Array = null; var pnt:MapPoint = null; var guid:String = UIDUtil.createUID(); var resultXml = "<?xml version='1.0' encoding='utf-8' standalone='yes'?><root><row>" + "<Task TaskID='"+guid+"'><AJ_DJ_GUID name='案件编号'></AJ_DJ_GUID><TaskName name='任务名称'></TaskName>" + "<AJ_MC name='案件名称'></AJ_MC><AJ_DD name='案件地点'></AJ_DD><SFHCWC name='是否核查完成'></SFHCWC><HCSJ name='核查时间'></HCSJ>" + "<RefPointX name='巡查地点X坐标'></RefPointX><RefPointY name='巡查地点Y坐标'></RefPointY><TaskDesc name='任务说明'>" + "</TaskDesc></Task><Project><AJ_DJ_GUID>"+guid+"</AJ_DJ_GUID><XZQDM></XZQDM><AJMC>"+ajmc+"</AJMC><BZ></BZ>" + "<AJDKS count='1'><AJDK><Attribute><DKBH></DKBH><DKMC>"+ajmc+"</DKMC><TDYT></TDYT><DKMJ></DKMJ></Attribute><Shape>"; if(this._arrPoints != null && _arrPoints.length>0){ for(var i:int = 0; i<_arrPoints.length;i++){ points = _arrPoints[i]; if(points!=null && points.length>0){ for (var j:int=0; j<points.length-1; j++){ pointNunber = j+1; pnt = MapPoint(points[j]); resultXml += "<Point NO='"+pointNunber+"' X='"+pnt.y.toFixed(4)+"' Y='"+pnt.x.toFixed(4)+"'/>"; } } } } resultXml +="</Shape><Appendix></Appendix></AJDK></AJDKS></Project></row></root>"; if(pointNunber >= 3){//是面状图形 callJsToExportXML(resultXml); } }
public static function callJsToExportXML(rsXml:String):void { var f:String = "exportToXml"; var rs:String = ExternalInterface.call(f,rsXml); if (rs == "fail"){ // promptMessage("导出Excel失败!",this); } }
2.1、前端代码(直接保存到本地)
function exportToXml(resultXml){ var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.loadXML(resultXml); var ajmc = xmlDoc.getElementsByTagName("AJMC")[0].childNodes[0].text; xmlDoc.save("D:\\"+ajmc+".xml"); alert("保存到【D:\\"+ajmc+".xml】成功!"); }
2.2弹窗选择保存路径
function exportToXml(resultXml){ try { var Message = "\u8bf7\u9009\u62e9\u6587\u4ef6\u5939"; //选择框提示信息 var Shell = new ActiveXObject("Shell.Application"); var Folder = Shell.BrowseForFolder(0, Message, 64, 17); //起始目录为:我的电脑 //var Folder = Shell.BrowseForFolder(0, Message, 0); //起始目录为:桌面 if (Folder != null) { Folder = Folder.items(); // 返回 FolderItems 对象 Folder = Folder.item(); // 返回 Folderitem 对象 Folder = Folder.Path; // 返回路径 if (Folder.charAt(Folder.length - 1) != "\\") { Folder = Folder + "\\"; } if(Folder != "" && Folder != "undefined"){ var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.loadXML(resultXml); var ajmc = xmlDoc.getElementsByTagName("AJMC")[0].childNodes[0].text; var filepath = Folder+ajmc+".xml"; var fso=new ActiveXObject("Scripting.FileSystemObject"); if(fso.FileExists(filepath)){ if(confirm('文件已存在,要替换吗?')){//选择是 xmlDoc.save(filepath); } }else{ xmlDoc.save(filepath); } } } } catch (e) { alert(e.message); } }
三、读取
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格式坐标失败,请检查格式是否正确。"); } }
多看一行书,就少写一行代码,记录点滴,用心生活。