接口開發之PHP建立XML文件

用PHP的DOM控件來建立輸出javascript

輸出的格式爲XMLphp

接口開發的相關文件及說明css

<?php
    header("Content-type: text/xml");//頭文件很是重要

    //建立xml文件
    $dom=new DOMDocument('1.0','utf-8');
    
    //創建<root>元素
    $root=$dom->createElement('root');
    
    //把<root>元素追加到文檔裏面
    $dom->appendChild($root);
    
    //創建<book>元素並將其做爲<root>的子元素
    $book=$dom->createElement('book');
    $root->appendChild($book);
    
    //接受GET過來的數據
    $key=$_GET['key'];
    $name=$_GET['name'];

    //引入數據庫配置文件
    include("config.inc.php");  

    //將get來的數據與數據庫裏的數據進行校驗
    $sql1=mysql_query("select count(id) as sum from user where `key` = ".$key."");
    
    //返回結果集
    $result=mysql_fetch_array($sql1);

    //若是數據庫裏存在key
    if($result[0]>0){
        //校驗name
        $sql2=mysql_query("select * from book where `name` = '".$name."'");
        $row=mysql_fetch_row($sql2);

        if(mysql_num_rows($sql2)>0){

            //爲<book>建立name元素,並追加name的值
            $name=$dom->createElement('name');
            $nameText=$dom->createTextNode($row[1]);
            $name->appendChild($nameText);
            $book->appendChild($name);
            
            //爲<book>建立author元素,並追加author的值
            $author=$dom->createElement('author');
            $authorText=$dom->createTextNode($row[2]);
            $author->appendChild($authorText);
            $book->appendChild($author);
            
            //爲<book>建立press元素,並追加press的值
            $press=$dom->createElement('press');
            $pressText=$dom->createTextNode($row[3]);
            $press->appendChild($pressText);
            $book->appendChild($press);
            
            //爲<book>建立price元素,並追加prcie的值
            $price=$dom->createElement('price');
            $priceText=$dom->createTextNode($row[4]);
            $price->appendChild($priceText);
            $book->appendChild($price);

            //爲<book>建立time元素,並追加time的值
            $time=$dom->createElement('time');
            $timeText=$dom->createTextNode($row[5]);
            $time->appendChild($timeText);
            $book->appendChild($time);

            //爲<book>建立intro元素,並追加intro的值
            $intro=$dom->createElement('intro');
            $introText=$dom->createTextNode($row[6]);
            $intro->appendChild($introText);
            $book->appendChild($intro);


        }else{//若是name不存在
            //創建<error_code>,並追加error_code的值
            $error_code=$dom->createElement('error_code');
            $error_code_Text=$dom->createTextNode('錯誤代碼1001');
            $error_code->appendChild($error_code_Text);
            $book->appendChild($error_code);
            
            //創建<result>,並追加result的值
            $result=$dom->createElement('result');
            $result_Text=$dom->createTextNode('書名不存在或錯誤');
            $result->appendChild($result_Text);
            $book->appendChild($result);

        }

    }else{//若是key不存在
        //創建<error_code>,並追加error_code的值
        $error_code=$dom->createElement('error_code');
        $error_code_Text=$dom->createTextNode('錯誤代碼1002');
        $error_code->appendChild($error_code_Text);
        $book->appendChild($error_code);
        
        //創建<result>,並追加result的值
        $result=$dom->createElement('result');
        $result_Text=$dom->createTextNode('密鑰錯誤或不存在');
        $result->appendChild($result_Text);
        $book->appendChild($result);
    }

    //在一字符串變量中創建XML結構
    $xmlText=$dom->saveXML();
    
    //輸出XML字符串
    echo $xmlText;
?>

傳參key=12345,name=C語言。  html

而後輸出的結果是:java

 

這裏是用戶用javascript調用xml文件node

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>獲取數據</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<script type="text/javascript">
window.onload=function(){     
        var btn=document.getElementById('btn');
        btn.onclick=function(){
            var title=document.getElementById('title').value;      
            var name,author,press,price,book;
            if(window.XMLHttpRequest){
                //code for IE+7,Firefox,Chrome,Opera,Safari
                xmlhttp=new XMLHttpRequest();
            }else{
                //code foe IE6,IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
            }
            url='http://localhost/api/index.php?key=12345&name='+title;
            //console.log(url);
            xmlhttp.open("GET",url,false);
            xmlhttp.send();
            xmldoc=xmlhttp.responseXML;           
            book=xmldoc.getElementsByTagName("book");
            error_code=xmldoc.getElementsByTagName("error_code");
            if(error_code.length==1){
                alert("書名錯誤或不存在");           
            }
            name=xmldoc.getElementsByTagName("name")[0];
            author=xmldoc.getElementsByTagName("author")[0];
            press=xmldoc.getElementsByTagName("press")[0];
            price=xmldoc.getElementsByTagName("price")[0];
            text="<table border='1'><tr><th>書名</th><th>做者</th><th>出版社</th><th>價格</th></tr>";
            text+="<tr>";
            text+="<td>"+name.firstChild.nodeValue+"</td>";
            text+="<td>"+author.firstChild.nodeValue+"</td>";
            text+="<td>"+press.firstChild.nodeValue+"</td>";
            text+="<td>"+price.firstChild.nodeValue+"</td>";
            text+="</tr>";
            text+="</table>";
            document.getElementById("text").innerHTML=text;
        } 
}
</script>
<style type="text/css">
</style>
</head>
    <body>
    書名:<input type="text" name="title" id="title"/>
    <input type="submit" id="btn" value="查詢"/>
    <div id="text"></div>
    </body>
</html>

  

這裏是接口開發的相關代碼:mysql

http://download.csdn.net/detail/yxhbk/9506715sql

這裏是接口開發說明文檔:數據庫

http://wenku.baidu.com/view/14706f8369eae009591bec44api

相關文章
相關標籤/搜索