jQuery解析XML
<?xml version="1.0" encoding="UTF-8"?> <root> <city>广州</city> <city>深圳</city> <city>香港</city> <city>澳门</city> <city>台湾</city> <city>中山</city> </root>
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>jQuery解析XML</title> <script type="text/javascript" src="js/jquery-1.8.2.js"></script> </head> <body> <input type="button" value="解析服务器响应的xml文件"/> <script type="text/javascript"> $(":button").click(function(){ var url = "${pageContext.request.contextPath}/03_city.xml?time="+new Date().getTime(); var sendData = null; $.get(url,sendData,function(xml){ //用jquery中的api解析xml文件,这时的xml是js对象 var $xml = $(xml).find("city"); //迭代 $xml.each(function(){ var city = $(this).text(); alert(city); }); }); }); </script> </body> </html>
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!