Home  >  Article  >  Web Front-end  >  AJAX processing method for XML returned by server

AJAX processing method for XML returned by server

亚连
亚连Original
2018-05-25 11:47:311884browse

This article mainly introduces the AJAX processing method of XML returned by the server. The example analyzes the implementation skills of Ajax and the processing method of XML return data. Friends in need can refer to the following

The examples of this article describe AJAX processing method for XML returned by the server. Share it with everyone for your reference. The specific analysis is as follows:

In AJAX, if the server side returns an XML document, it can obtain the XML data through the responseXML attribute of the asynchronous object. Developers can use DOM related methods to process it.

Assume that the XML document returned by the server is as follows:

<?xml version="1.0" encoding="gb2312"?>
<list>
 <caption>Member List</caption>
 <member>
  <name>isaac</name>
  <class>W13</class>
  <birth>Jun 24th</birth>
  <constell>Cancer</constell>
  <mobile>1118159</mobile>
 </member>
 <member>
  <name>fresheggs</name>
  <class>W610</class>
  <birth>Nov 5th</birth>
  <constell>Scorpio</constell>
  <mobile>1038818</mobile>
 </member>
 <member>
  <name>girlwing</name>
  <class>W210</class>
  <birth>Sep 16th</birth>
  <constell>Virgo</constell>
  <mobile>1307994</mobile>
 </member>
 <member>
  <name>tastestory</name>
  <class>W15</class>
  <birth>Nov 29th</birth>
  <constell>Sagittarius</constell>
  <mobile>1095245</mobile>
 </member>
 <member>
  <name>lovehate</name>
  <class>W47</class>
  <birth>Sep 5th</birth>
  <constell>Virgo</constell>
  <mobile>6098017</mobile>
 </member>
 <member>
  <name>slepox</name>
  <class>W19</class>
  <birth>Nov 18th</birth>
  <constell>Scorpio</constell>
  <mobile>0658635</mobile>
 </member>
 <member>
  <name>smartlau</name>
  <class>W19</class>
  <birth>Dec 30th</birth>
  <constell>Capricorn</constell>
  <mobile>0006621</mobile>
 </member>
 <member>
  <name>tuonene</name>
  <class>W210</class>
  <birth>Nov 26th</birth>
  <constell>Sagittarius</constell>
  <mobile>0091704</mobile>
 </member>
 <member>
  <name>dovecho</name>
  <class>W19</class>
  <birth>Dec 9th</birth>
  <constell>Sagittarius</constell>
  <mobile>1892013</mobile>
 </member>
 <member>
  <name>shanghen</name>
  <class>W42</class>
  <birth>May 24th</birth>
  <constell>Gemini</constell>
  <mobile>1544254</mobile>
 </member>
 <member>
  <name>venessawj</name>
  <class>W45</class>
  <birth>Apr 1st</birth>
  <constell>Aries</constell>
  <mobile>1523753</mobile>
 </member>
 <member>
  <name>lightyear</name>
  <class>W311</class>
  <birth>Mar 23th</birth>
  <constell>Aries</constell>
  <mobile>1002908</mobile>
 </member>
</list>

The client obtains the XML data from the server and displays it in in the table. The code is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>responseXML</title>
<style>
<!--
.datalist{
 border:1px solid #744011; /* 表格边框 */
 font-family:Arial;
 border-collapse:collapse; /* 边框重叠 */
 background-color:#ffd2aa; /* 表格背景色 */
 font-size:14px;
}
.datalist th{
 border:1px solid #744011; /* 行名称边框 */
 background-color:#a16128; /* 行名称背景色 */
 color:#FFFFFF;    /* 行名称颜色 */
 font-weight:bold;
 padding-top:4px; padding-bottom:4px;
 padding-left:12px; padding-right:12px;
 text-align:center;
}
.datalist td{
 border:1px solid #744011; /* 单元格边框 */
 text-align:left;
 padding-top:4px; padding-bottom:4px;
 padding-left:10px; padding-right:10px;
}
.datalist tr:hover, .datalist tr.altrow{
 background-color:#dca06b; /* 动态变色 */
}
input{ /* 按钮的样式 */
 border:1px solid #744011;
 color:#744011;
}
-->
</style>
<script language="javascript">
var xmlHttp;
function createXMLHttpRequest(){
 if(window.ActiveXObject)
  xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
 else if(window.XMLHttpRequest)
  xmlHttp = new XMLHttpRequest();
}
function getXML(addressXML){
 var url = addressXML + "?timestamp=" + new Date();
 createXMLHttpRequest();
 xmlHttp.onreadystatechange = handleStateChange;
 xmlHttp.open("GET",url);
 xmlHttp.send(null);
}
function addTableRow(sName, sClass, sBirth, sConstell, sMobile){
 //表格添加一行的相关操作,可参看7.2.1节
 var oTable = document.getElementById("member");
 var oTr = oTable.insertRow(oTable.rows.length);  
 var aText = new Array();
 aText[0] = document.createTextNode(sName);
 aText[1] = document.createTextNode(sClass);
 aText[2] = document.createTextNode(sBirth);
 aText[3] = document.createTextNode(sConstell);
 aText[4] = document.createTextNode(sMobile);
 for(var i=0;i<aText.length;i++){
  var oTd = oTr.insertCell(i);
  oTd.appendChild(aText[i]);
 }
}
function DrawTable(myXML){
 //用DOM方法操作XML文档
 var oMembers = myXML.getElementsByTagName("member");
 var oMember = "", sName = "", sClass = "", sBirth = "", sConstell = "", sMobile = "";
 for(var i=0;i<oMembers.length;i++){
  oMember = oMembers[i];
  sName = oMember.getElementsByTagName("name")[0].firstChild.nodeValue;
  sClass = oMember.getElementsByTagName("class")[0].firstChild.nodeValue;
  sBirth = oMember.getElementsByTagName("birth")[0].firstChild.nodeValue;
  sConstell = oMember.getElementsByTagName("constell")[0].firstChild.nodeValue;
  sMobile = oMember.getElementsByTagName("mobile")[0].firstChild.nodeValue;
  //添加一行
  addTableRow(sName, sClass, sBirth, sConstell, sMobile);
 }
}
function handleStateChange(){  
 if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
  DrawTable(xmlHttp.responseXML);//responseXML获取到XML文档
}
</script>
</head>
<body>
<input type="button" value="获取XML" onclick="getXML(&#39;9-4.xml&#39;);"><br><br>
<table class="datalist" summary="list of members in EE Studay" id="member">
 <tr>
  <th scope="col">Name</th>
  <th scope="col">Class</th>
  <th scope="col">Birthday</th>
  <th scope="col">Constellation</th>
  <th scope="col">Mobile</th>
 </tr>
</table>
</body>
</html>

We can see that the code to obtain the XML file on the client is as follows:

<input type="button" value="获取XML" onclick="getXML(&#39;9-4.xml&#39;);">

In other words, the XML data is obtained directly. In actual development, the work of returning XML data is dynamically generated through server-side (such as ASP.NET, JSP, etc.) code. In other words, the file address in getXML('...') should be the suffix of a dynamic page such as .aspx or .jsp.

Use jQuery framework to implement

If you use jQuery framework on the client, implement AJAX to obtain the XML data on the server side.

The code is as follows:



  demo 
 
 
 


 <input type="button" value="获取XML" onclick="getXML(&#39;9-4.xml&#39;);">
 
Name Class Birthday Constellation Mobile

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Detailed explanation of ajax synchronization and asynchronousness in jquery

How to implement ASP.NET and Ajax

How JQuery ajax returns JSON

The above is the detailed content of AJAX processing method for XML returned by server. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn