当前位置: 代码迷 >> Java Web开发 >> 求救:xml解析到jsp上解决思路
  详细解决方案

求救:xml解析到jsp上解决思路

热度:503   发布时间:2016-04-17 12:15:28.0
求救:xml解析到jsp上

 现有一xml文件

<?xml version="1.0" encoding="GBK" ?>
<sysconfig> 
 <config key="starturl">asd</config> 
 <config key="mmfiledir" >12</config> 
 <config key="wincfgdir" >331</config>  
</sysconfig>

解析到页面
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="1" class="tableStyle01">
  <tr> 
  <td align="right" class="TDstyle01">fileurl: </td>
  <td class="TDstyle01"><input name="fileurl" type="text" class="input" ></td>
  </tr>
  <tr> 
  <td align="right" class="TDstyle01">starturl: </td>
  <td class="TDstyle01"><input name="starturl" type="text" class="input" ></td>
  </tr>
  <tr> 
  <td align="right" class="TDstyle01">mmfiledir:</td>
  <td class="TDstyle01"><input name="mmfiledir" type="text" class="input" style="width:80%" ></td>
  </tr>
</table>

------解决方案--------------------
楼主是想解析xml的内容然后填入页面上相应的文本框吗?
方式有很多啊
1,用一个servlet,在servlet里面进行xml的解析,然后将解析的结果进行返回,在这个页面进行取得所需数据就可以了
2,在页面加载的时候用js的方式进行xml的解析,并对text控件进行赋值
------解决方案--------------------
探讨
楼主是想解析xml的内容然后填入页面上相应的文本框吗?
方式有很多啊
1,用一个servlet,在servlet里面进行xml的解析,然后将解析的结果进行返回,在这个页面进行取得所需数据就可以了
2,在页面加载的时候用js的方式进行xml的解析,并对text控件进行赋值

------解决方案--------------------
1楼2楼说了不是等于没说,你这样说LZ知道怎么做吗~~
帮LZ写了个在JS里直接解析的,不过你的XML中的三个key的名字跟文本框的有一个不一样,就把wincfgdir里的值给你写到文本框fileurl里去了~
xmlDoc.load("text.xml");文件的路径写绝对路径也可以,这样写是该文件与XML文件在同一目录下~如果你这程序要用在客户端的话最好写绝对路径,示例代码如下:
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML> <HEAD>  <TITLE> New Document </TITLE>  <META NAME="Generator" CONTENT="EditPlus">  <META NAME="Author" CONTENT="">  <META NAME="Keywords" CONTENT="">  <META NAME="Description" CONTENT="">  <script language="javascript">      function readXML(){          var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");          xmlDoc.async="false";          xmlDoc.load("text.xml");          var code=xmlDoc.getElementsByTagName("config");          for(var i=0;i<code.length;i++){              var nodename = code[i].getAttribute("key");               var nodevalue = code[i].firstChild.nodeValue;              if(nodename == "starturl"){                  document.getElementById("record2").value=nodevalue;              }else if(nodename == "mmfiledir"){                  document.getElementById("record3").value=nodevalue;              }else if(nodename == "wincfgdir"){                  document.getElementById("record1").value=nodevalue;              }          }      }  </script> </HEAD> <BODY onload="readXML();">  <form name="form1" >  <table width="80%" border="0" align="center" cellpadding="2" cellspacing="1" class="tableStyle01">   <tr>     <td align="right" class="TDstyle01">fileurl: </td>     <td class="TDstyle01"> <input name="fileurl" type="text" class="input" id="record1" > </td>   </tr>   <tr>     <td align="right" class="TDstyle01">starturl: </td>     <td class="TDstyle01"> <input name="starturl" type="text" class="input" id="record2" > </td>   </tr>   <tr>     <td align="right" class="TDstyle01">mmfiledir: </td>     <td class="TDstyle01"> <input name="mmfiledir" type="text" class="input" style="width:80%" id="record3"> </td>   </tr> </table> </BODY> </form></HTML>
  相关解决方案