這篇文章主要介紹了ajax遍歷xml文檔的方法,實例分析了jsp結合Ajax遍歷XML文檔的實現技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了ajax遍歷xml文檔的方法。分享給大家供大家參考。具體分析如下:
XMLHttpRequest物件提供了兩個可以用來存取伺服器回應的屬性。第一個屬性responseText將回應提供為一個字串,第二個屬性 responseXML將回應提供為一個XML物件。一些簡單的用例就很適合按簡單文字來獲取回應,如將回應顯示在警告框中,或者回應只是指示成功還是失敗的字
前面85b7cb3f730c0edb8395188aa547d544中的例子是從XMLHttpRequest物件取得伺服器回應,並使用XMLHttpRequest物件的responseText屬性將回應取得為文字。
這次我們來使用XMLHttpRequest物件的responseXML屬性,將結果取得為XML文件.這樣一來,我們就可以使用W3C DOM方法來遍歷XML文件。 (前面文章或多或少講過些DOM,在此不重複)
OK,下面來看範例.
#首先還是一段XML文件程式碼(parseXML.xml)如下:
parseXML.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <states> <north> <state>Minnesota</state> <state>Iowa</state> <state>North Dakota</state> </north> <south> <state>Texas</state> <state>Oklahoma</state> <state>Louisiana</state> </south> <east> <state>New York</state> <state>North Carolina</state> <state>Massachusetts</state> </east> <west> <state>California</state> <state>Oregon</state> <state>Nevada</state> </west> </states>
#MyJsp.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <script type="text/javascript"> var flg=false; var requestType = ""; //得到XMLHttpRequest对象 function newXMLHttpRequest() { var xmlreq = false; if (window.XMLHttpRequest) { xmlreq = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlreq = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e1) { try { xmlreq = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) {} } } return xmlreq; } function startRequest(requestedList){ requestType=requestedList; flg=newXMLHttpRequest(); //当XMLHttpRequest对象在请求过程中间状态改变的时候 //回来调用handleStateChange方法 flg.onreadystatechange = handleStateChange; flg.open("GET", "parseXML.xml", true); flg.send(null); } //处理函数 function handleStateChange(){ if(flg.readyState==4){ if(flg.status==200){ if(requestType=="north"){ listNorthStates(); }else if(requestType=="all"){ listAllStates(); }if(requestType=="south"){ listSouthStates(); } } } } //用于显示NorthStates方法 function listNorthStates(){ var xmlDoc=flg.responseXML; var northNode=xmlDoc.getElementsByTagName("north")[0]; var northStates=northNode.getElementsByTagName("state"); outputList("North States",northStates); } //用于显示SouthStates方法 function listSouthStates(){ var xmlDoc=flg.responseXML; var SouthNode=xmlDoc.getElementsByTagName("south")[0]; var SouthStates=SouthNode.getElementsByTagName("state"); outputList("South States",SouthStates); } //用于显示AllStates方法 function listAllStates(){ var xmlDoc=flg.responseXML; var allStates=xmlDoc.getElementsByTagName("state"); outputList("All States in Document", allStates); } //输出元素并显示于提示框中 function outputList(title,states){ var out=title; var currState=null; for(var i=0;i<states.length;i++){ currState=states; out=out+"\n-"+currState.childNodes[0].nodeValue; } alert(out); } </script> <body> <form action="#"> <input type="button" value="View All Listed States" onclick="startRequest('all');"/><br> <input type="button" value="View All Listed Northern States" onclick="startRequest('north');"/><br> <input type="button" value="View All Listed Southern States" onclick="startRequest('south');"/> </form> </body> </html>
#以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
以上是關於ajax遍歷xml文檔的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!