|
80端口悄悄提示:数据载入中…… |
|
PHP代码
- class array2xml {
- var $xml;
- function array2xml($array,$encoding='utf-8') {
- $this->xml='<?xml version="1.0" encoding="'.$encoding.'"?><root>';
- $this->xml.=$this->_array2xml($array);
-
- }
- function getXml() {
- return $this->xml.'</root>';
- }
- function _array2xml($array) {
- foreach($array as $key=>$val) {
- $num=is_numeric($key);
- $num&&$key="item id=\"$key\"";
- $xml.="<$key>";
- $num||$xml.="<![CDATA[";
- $xml.=is_array($val)?$this->_array2xml($val):$val;
- list($key,)=explode(' ',$key);
- $num||$xml.="]]></$key>";
- $num&&$xml.="</item>";
- }
- return $xml;
- }
- }
实例:
PHP代码
- function responseXML($sql)
- {
- global $DB;
- $query=$DB->query($sql);
- $arr=array();
- while($r=$DB->fetch_array($query)){
- $arr[]=$r;
- }
- unset($r);
- $DB->free_result($query);
- if(count($arr)>0){
- $ax=new array2xml($arr);
- exit($ax->getXML());
- }else{
- out_ok('null');
- }
- }
方法二: 输出xml
PHP代码
- <?
- $link=mysql_connect("localhost","root","");
- mysql_select_db("marry");
- if($link){
- $result=mysql_query("select * from marry_member");
- $result1=mysql_query("show columns from marry_member");
- $columns=array();
- while($r=mysql_fetch_row($result1)){
- $columns[]=$r[0];
- }
- mysql_free_result($result1);
- header("content-type:text/xml");
- echo "<?xml version='1.0' encoding='UTF-8'?>";
- echo "<xml>";
- while($arr=mysql_fetch_array($result))
- {
- echo "<row";
- $i = 0;
- for($j=0;$j<count($columns);$j++){
- echo " ".$columns[$j]."='".$arr[$i]."' ";
- $i++;
- }
- reset($columns);
- echo "/>";
- }
- echo "</xml>";
-
- mysql_free_result($result);
- }
- ?>
|
|