Home > Backend Development > PHP Tutorial > PHP generates and obtains XML format data_PHP tutorial

PHP generates and obtains XML format data_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-20 10:58:27
Original
786 people have browsed it

When making data interfaces, we usually need to obtain third-party data interfaces or provide data interfaces to third parties, and these data formats are usually transmitted in XML or JSON format. This article will introduce how to use PHP Generate XML format data for third-party calls and how to obtain XML data provided by third parties.

Generate XML format data

We assume that there is a student information table student in the system, which needs to be provided to a third party for calls, and has id, name, sex, and age respectively. Record the student's name, gender, age and other information.

<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>
Copy after login

First, create the createXML.php file, connect to the database first, and obtain the data.

<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>
Copy after login

At this time, the data is saved in $arr. You can use print_r to print the data for testing.

Next, create xml, loop the array, and write data to the node corresponding to 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>
Copy after login

We called PHP’s built-in class DOMDocument to process and generate xml. The final generated xml format please

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445684.htmlTechArticleWhen making data interfaces, we usually need to obtain third-party data interfaces or provide data interfaces to third parties, and these The data format is usually transmitted in XML or JSON format. This article will introduce such as...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template