PHP generates and obtains XML format data implementation code

Source: Internet
Author: User
Tags php file

Generate XML Format data

We hypothesized that there is a student information table student in the system, need to provide to the third party call, and have id,name,sex,age record the student's name, gender, age and so on separately.

The code is as follows Copy Code

CREATE TABLE ' Student ' (
' id ' int (one) not NULL auto_increment,
' Name ' varchar not NULL,
' Sex ' varchar not NULL,
' Age ' smallint (3) Not NULL default ' 0 ',
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8;

First, establish the createxml.php file, connect the database first, obtain the data.

  code is as follows copy code

Include_once ("connect.php"); Connecting to a database

$sql = "SELECT * from student";
$result = mysql_query ($sql) or Die ("Invalid query:". Mysql_error ());
while ($row = Mysql_fetch_array ($result)) {
$arr [] = Array (
' Name ' => $row [' name '],
' Sex ' => $row [' Sex '],
' Age ' => $row [' age ']
);
}

This time, the data is stored in the $arr, you can use Print_r print the data test.

Next, create XML, loop arrays, and write data to the corresponding nodes in the XML.

The code is as follows Copy Code

$doc = new DOMDocument (' 1.0 ', ' utf-8 '); Declaring versions and encodings
$doc->formatoutput = true;

$r = $doc->createelement ("root");
$doc->appendchild ($R);

foreach ($arr as $dat) {
$b = $doc->createelement ("data");

$name = $doc->createelement ("name");
$name->appendchild ($doc->createtextnode ($dat [' name ')]);
$b->appendchild ($name);

$sex = $doc->createelement ("sex");
$sex->appendchild ($doc->createtextnode ($dat [' sex ']));
$b->appendchild ($sex);

$age = $doc->createelement ("Age");
$age->appendchild ($doc->createtextnode ($dat [' age ']));
$b->appendchild ($age);

$r->appendchild ($b);
}

echo $doc->savexml ();

We call PHP's built-in class DOMDocument to process and generate XML. The resulting XML format please click here to see the effect

The code is as follows Copy Code

<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<data>
<name> Li Wanghao </name>
<sex> male </sex>
<age>21</age>
</data>
...
</root>

Get XML Format data

Now we're going to assume that to get student information from a third party, the data format is XML, we need to parse the XML using PHP, and then display the parsed data or write it to the local database. And the key step here is parsing XML.

PHP has a number of ways to parse XML, where PHP provides a built-in XmlReader class that allows you to sequentially browse through the XML files, and you can imagine a cursor walking through the entire file node and grabbing the desired content. Using XmlReader is efficient, especially when reading very large XML data, and compared to other methods, using XmlReader consumes very little memory.

The code is as follows Copy Code

Header ("content-type:text/html; Charset=utf-8 ");
$url = "importxml/createxml.php";
$reader = new XMLReader (); Instantiation of XmlReader
$reader->open ($url); Get XML
$i = 1;
while ($reader->read ()) {
if ($reader->nodetype = = Xmlreader::text) {//Determine node type
$m = $i% 3;
if ($m ==1)
$name = $reader->value; Read node value
if ($m ==2)
$sex = $reader->value;
if ($m ==0) {
$age = $reader->value;
$arr [] = Array (
' Name ' => $name,
' Sex ' => $sex,
' Age ' => $age
);
}
$i + +;
}
}
Print_r ($arr);

In order to separate the data name,sex from the age, we use $i%3 to determine the modulo, because the information under node data is present in the acquired XML as 3 subnodes.

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.