xml文件:
1 <?xml version="1.0" encoding="gb2312"?> 2 <china> 3 <province name="吉林省"> 4 <city>长春</city> 5 <city>吉林市</city> 6 <city>四平</city> 7 <city>松原</city> 8 <city>通化</city> 9 </province> 10 <province name="辽宁省"> 11 <city>沈阳</city> 12 <city>大连</city> 13 <city>鞍山</city> 14 <city>抚顺</city> 15 <city>铁岭</city> 16 </province> 17 <province name="山东省"> 18 <city>济南</city> 19 <city>青岛</city> 20 <city>威海</city> 21 <city>烟台</city> 22 <city>廊坊</city> 23 </province> 24 </china>
jsp代码:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <script type="text/javascript" src="jquery-2.1.1.js"></script> 8 </head> 9 <body> 10 </body> 11 <script type="text/javascript"> 12 $(document).ready(function() { 13 14 $.get("cities.xml",function(xmlDoc,textStatus){ 15 //find搜索所有与指定表达式匹配的元素 16 // alert(xmlDoc); 17 var $provinceElement=$(xmlDoc).find("province"); 18 // alert($provinceElement.length); 19 $provinceElement.each(function(index,domEle) { 20 var $city=$(this).find("city"); 21 // alert($city.length); 22 $.each($city,function(index,domEle1){ 23 24 alert($(domEle1).text()); 25 }); 26 }); 27 }); 28 }); 29 </script> 30 </html>
时间: 2024-11-02 11:27:20