<div class="codetitle"> <span><a style="CURSOR: pointer" data="25768" class="copybut" id="copybut25768" onclick="doCopy('code25768')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code25768"> <br> <br> <br><title>jquery xml解析</title> <br><script src="jquery.min.js" type="text/javascript"></script> <br><script type="text/javascript"> <BR>$(document).ready(function(){ <BR>$.ajax({url:"City.xml", <BR>success:function(xml){ <BR>$(xml).find("province").each(function(){ <BR>var t = $(this).attr("name");//this-> <BR>$("#DropProvince").append("<option>"+t+""); <BR>}); <BR>} <BR>}); <BR>$("#DropProvince").change(function(){ <BR>$("#sCity>option").remove(); <BR>var pname = $("#DropProvince").val(); <BR>$.ajax({url:"City.xml", <BR>success:function(xml){ <BR>$(xml).find("province[name='"+pname+"']>city").each(function(){ <BR>$("#sCity").append("<option>"+$(this).text()+""); <BR>}); <BR>} <BR>}); <BR>}); <BR>}); <BR></script> <br> <br> <br><form id="form1"> <br><div> <br><select id="DropProvince" style="width:60px;"> <br><option>请选择</option> <br></select> <br><select id="sCity" style="width:60px;"> <br></select> <br> </div> <br> </form> <br> <br> <br> </div> <br><strong>city.xml文件 <br></strong><div class="codetitle"> <span><a style="CURSOR: pointer" data="17482" class="copybut" id="copybut17482" onclick="doCopy('code17482')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code17482"> <br><?xml version="1.0" encoding="utf-8" ?> <br><provinces> <br><province name="湖北"> <br><city>武汉</city> <br><city>黄石</city> <br><city>宜昌</city> <br><city>天门</city> <br></province> <br><province name="湖南"> <br><city>邵阳</city> <br><city>长沙</city> <br><city>岳阳</city> <br></province> <br><province name="广东"> <br><city>广州</city> <br><city>深圳</city> <br></province> <br></provinces><br> </div> <br>其实主要是注意怎样在html界面上解析xml文件,格式就是 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="95456" class="copybut" id="copybut95456" onclick="doCopy('code95456')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code95456"> <br><script type="text/javascript"> <BR>$(document).ready(function () { <BR>$.ajax({ <BR>url: "City.xml", <BR>success: function (xml) { <BR>$(xml).find("province").each(function () { <BR>var t = $(this).attr("name"); <BR>$("#DropProvince").append("<option>" + t + ""); <BR>}); <BR>} <BR>}); <BR>$("#DropProvince").change(function () { <BR>$("#sCity>option").remove(); <BR>var pname = $("#DropProvince").val(); <BR>$.ajax({ <BR>url: "City.xml", <BR>success: function (xml) { <BR>$(xml).find("province[name='"+pname+"']>city").each(function(){ <BR>$("#sCity").append("<option>"+$(this).text()+""); <BR>}); <BR>} <BR>}); <BR>}); <BR>}); <BR></script> <br> </div> <br>就是用$.ajax()调用xml文件的内容。然后$.each()进行循环操作,基本思想就是这样的,成功之后去执行success这个回调函数。这里的xml文件是用来存储数据的,相当于在数据库中读取文件。