接口开发之PHP创建XML文件

用PHP的DOM控件来创建输出

输出的格式为XML

接口开发的相关文件及说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?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语言。  

然后输出的结果是:

 

这里是用户用javascript调用xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!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>

  

这里是接口开发的相关代码:

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

这里是接口开发说明文档:

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

 

posted @   Yxh_blogs  阅读(4123)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示