XML
用XML传输数据:
代码:
1.xml.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="jquery-3.2.0.min.js"></script> </head> <body> HTML:超文本标记语言,主要用来展示 XML:可扩展标记语言,用来做数据传输,数据存储 特点: 1.树状结构,有且只有一个根 2.标签名自定义 3.标签名很敏感,都是双标签元素 4.同一级下不能出现重名标签 <!--<nation> <n1> <code>n001</code> <name>汉族</name> </n1> <n2> <code>n002</code> <name>壮族</name> </n2> <n3> <code>n003</code> <name>维吾尔族</name> </n3> --> <div id="shuju"></div> </body> <script type="text/javascript"> $.ajax({ url:"xmlchuli.php", dataType:"XML", success: function(data){ var zi = $(data).find("nation").children(); var str =""; for(var i=0;i<zi.length;i++) { //zi.eq(i).find("code").text(); var nno = zi.eq(i).find("nno").text(); str = str+"<div>"+nno+"</div>"; } $("#shuju").html(str); } }) </script> </html>
2.xmlchuli.php
<?php require "DBDA.class.php"; $db = new DBDA(); $sql ="select * from nation"; $arr = $db->query($sql,1); echo "<nation>"; foreach($arr as $k=>$v) { echo "<n{$k}><code>{$v[0]}</code><nno>{$v[1]}</nno></n{$k}>"; }; echo "</nation>";