PHP generate and retrieve XML format data _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP generates and obtains XML format data. When using data interfaces, we usually need to obtain third-party data interfaces or provide third-party data interfaces. these data formats are generally transmitted in XML or JSON format.

When using data interfaces, we usually need to obtain third-party data interfaces or provide third-party data interfaces. these data formats are usually transmitted in XML or JSON format, this article describes how to use PHP to generate XML format data for third-party calls and how to obtain XML data provided by a third-party.

Generate XML format data

Assume that the system has a student info table student, which must be provided to a third party for calling. the student name, gender, age, and other information are recorded by id, name, sex, and age.

 
 
  1. CREATE TABLE `student` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `name` varchar(50) NOT NULL,
  4. `sex` varchar(10) NOT NULL,
  5. `age` smallint(3) NOT NULL default '0',
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

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

 
 
  1. Include_once ("connect. php"); // connect to the database
  2. $ SQL = "select * from student ";
  3. $ Result = mysql_query ($ SQL) or die ("Invalid query:". mysql_error ());
  4. While ($ row = mysql_fetch_array ($ result )){
  5. $ Arr [] = array (
  6. 'Name' => $ row ['name'],
  7. 'Sex '=> $ row ['Sex'],
  8. 'Age' => $ row ['age']
  9. );
  10. }

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

Then, create an xml and circular array to write data to the corresponding xml node.

 
 
  1. $ Doc = new DOMDocument ('1. 0', 'utf-8'); // declare the version and encoding
  2. $ Doc-> formatOutput = true;
  3. $ R = $ doc-> createElement ("root ");
  4. $ Doc-> appendChild ($ r );
  5. Foreach ($ arr as $ dat ){
  6. $ B = $ doc-> createElement ("data ");
  7. $ Name = $ doc-> createElement ("name ");
  8. $ Name-> appendChild ($ doc-> createTextNode ($ dat ['name']);
  9. $ B-> appendChild ($ name );
  10. $ Sex = $ doc-> createElement ("sex ");
  11. $ Sex-> appendChild ($ doc-> createTextNode ($ dat ['sex']);
  12. $ B-> appendChild ($ sex );
  13. $ Age = $ doc-> createElement ("age ");
  14. $ Age-> appendChild ($ doc-> createTextNode ($ dat ['age']);
  15. $ B-> appendChild ($ age );
  16. $ R-> appendChild ($ B );
  17. }
  18. Echo $ doc-> saveXML ();

We call the PHP built-in class DOMDocument to process and generate xml. The final generated xml format is as follows:

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.