PHP生成和获取XML格式数据_PHP教程

WBOY
发布: 2016-07-20 10:58:27
原创
748 人浏览过

在做数据接口时,我们通常要获取第三方数据接口或者给第三方提供数据接口,而这些数据格式通常是以XML或者JSON格式传输,本文将介绍如何使用PHP生成XML格式数据供第三方调用以及如何获取第三方提供的XML数据。

生成XML格式数据

我们假设系统中有一张学生信息表student,需要提供给第三方调用,并有id,name,sex,age分别记录学生的姓名、性别、年龄等信息。

<ol class="dp-xml">
<li class="alt"><span><span>CREATE TABLE `student` (   </span></span></li>
<li><span>  `id` int(11) NOT NULL auto_increment,   </span></li>
<li class="alt"><span>  `name` varchar(50) NOT NULL,   </span></li>
<li><span>  `sex` varchar(10) NOT NULL,   </span></li>
<li class="alt"><span>  `age` smallint(3) NOT NULL default '0',   </span></li>
<li><span>  PRIMARY KEY  (`id`)   </span></li>
<li class="alt">
<span>) </span><span class="attribute">ENGINE</span><span>=</span><span class="attribute-value">MyISAM</span><span>  DEFAULT </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">utf8</span><span>;   </span>
</li>
</ol>
登录后复制

首先,建立createXML.php文件,先连接数据库,获取数据。

<ol class="dp-sql">
<li class="alt"><span><span>include_once (</span><span class="string">"connect.php"</span><span>); //连接数据库    </span></span></li>
<li>
<span>$sql = </span><span class="string">"select * from student"</span><span>;   </span>
</li>
<li class="alt">
<span>$result = mysql_query($sql) </span><span class="op">or</span><span> die(</span><span class="string">"Invalid query: "</span><span> . mysql_error());   </span>
</li>
<li><span>while ($row = mysql_fetch_array($result)) {   </span></li>
<li class="alt"><span>    $arr[] = array(   </span></li>
<li>
<span>        </span><span class="string">'name'</span><span> => $row[</span><span class="string">'name'</span><span>],   </span>
</li>
<li class="alt">
<span>        </span><span class="string">'sex'</span><span> => $row[</span><span class="string">'sex'</span><span>],   </span>
</li>
<li>
<span>        </span><span class="string">'age'</span><span> => $row[</span><span class="string">'age'</span><span>]   </span>
</li>
<li class="alt"><span>    );   </span></li>
<li><span>}   </span></li>
</ol>
登录后复制

这个时候,数据就保存在$arr中,你可以使用print_r打印下数据测试。

接着,建立xml,循环数组,将数据写入到xml对应的节点中。

<ol class="dp-c">
<li class="alt"><span><span class="vars">$doc</span><span> = </span><span class="keyword">new</span><span> DOMDocument(</span><span class="string">'1.0'</span><span>, </span><span class="string">'utf-8'</span><span>);  </span><span class="comment">// 声明版本和编码  </span><span> </span></span></li>
<li>
<span class="vars">$doc</span><span>->formatOutput = true;    </span>
</li>
<li class="alt">
<span class="vars">$r</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"root"</span><span>);   </span>
</li>
<li>
<span class="vars">$doc</span><span>->appendChild(</span><span class="vars">$r</span><span>);    </span>
</li>
<li class="alt">
<span class="keyword">foreach</span><span> (</span><span class="vars">$arr</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$dat</span><span>) {   </span>
</li>
<li>
<span>    </span><span class="vars">$b</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"data"</span><span>);    </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$name</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"name"</span><span>);   </span>
</li>
<li>
<span>    </span><span class="vars">$name</span><span>->appendChild(</span><span class="vars">$doc</span><span>->createTextNode(</span><span class="vars">$dat</span><span>[</span><span class="string">'name'</span><span>]));   </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$b</span><span>->appendChild(</span><span class="vars">$name</span><span>);    </span>
</li>
<li>
<span>    </span><span class="vars">$sex</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"sex"</span><span>);   </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$sex</span><span>->appendChild(</span><span class="vars">$doc</span><span>->createTextNode(</span><span class="vars">$dat</span><span>[</span><span class="string">'sex'</span><span>]));   </span>
</li>
<li>
<span>    </span><span class="vars">$b</span><span>->appendChild(</span><span class="vars">$sex</span><span>);    </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$age</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"age"</span><span>);   </span>
</li>
<li>
<span>    </span><span class="vars">$age</span><span>->appendChild(</span><span class="vars">$doc</span><span>->createTextNode(</span><span class="vars">$dat</span><span>[</span><span class="string">'age'</span><span>]));   </span>
</li>
<li class="alt">
<span>    </span><span class="vars">$b</span><span>->appendChild(</span><span class="vars">$age</span><span>);    </span>
</li>
<li>
<span>    </span><span class="vars">$r</span><span>->appendChild(</span><span class="vars">$b</span><span>);   </span>
</li>
<li class="alt"><span>}    </span></li>
<li>
<span class="func">echo</span><span> </span><span class="vars">$doc</span><span>->saveXML();   </span>
</li>
</ol>
登录后复制

我们调用了PHP内置的类DOMDocument来处理与生成xml。最终生成的xml格式请

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445684.htmlTechArticle在做数据接口时,我们通常要获取第三方数据接口或者给第三方提供数据接口,而这些数据格式通常是以XML或者JSON格式传输,本文将介绍如...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板