This article illustrates how PHP generates and obtains XML-formatted data. Share to everyone for your reference, specific as follows:
When doing data interfaces, we typically get Third-party data interfaces or provide data interfaces to third parties, which are typically transmitted in XML or JSON format, which describes how to use PHP to generate XML format data for third party calls and how to obtain XML data from Third-party providers.
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.
CREATE TABLE ' student ' (
' id ' int (one) not null auto_increment,
' name ' varchar ' is not NULL,
' sex ' varchar (10) 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.
Include_once ("connect.php"); Connection 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.
$doc = new DOMDocument (' 1.0 ', ' utf-8 '); Declaring version and encoding
$doc->formatoutput = true;
$r = $doc->createelement_x ("root");
$doc->appendchild ($r);
foreach ($arr as $dat) {
$b = $doc->createelement_x ("data");
$name = $doc->createelement_x ("name");
$name->appendchild ($doc->createtextnode ($dat [' name ')]);
$b->appendchild ($name);
$sex = $doc->createelement_x ("sex");
$sex->appendchild ($doc->createtextnode ($dat [' sex ']));
$b->appendchild ($sex);
$age = $doc->createelement_x ("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
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<data>
<name> Li Wanghao </name>
<sex> men </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.
Header ("content-type:text/html; Charset=utf-8 ");
$url = "http://www.helloweba.com/demo/importXML/createXML.php";
$reader = new XMLReader (); Instantiate XmlReader
$reader->open ($url);//Get XML
$i =1;
while ($reader->read ()) {
if ($reader->nodetype = = Xmlreader::text) {//Judge 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.
More about PHP Interested readers can view the site topics: "PHP for XML file Operation skills Summary", "PHP date and Time usage summary", "PHP object-oriented Program Design Primer", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary
I hope this article will help you with the PHP program design.