PHP generate and get XML format data method, PHP get XML format _php tutorial

Source: Internet
Author: User

Php method for generating and retrieving XML format data, PHP gets XML format


The examples in this article describe how PHP generates and obtains XML format data. Share to everyone for your reference, as follows:

When we do data interfaces, we usually get third-party data interfaces or provide data interfaces to third parties, which are typically transmitted in XML or JSON format, where we'll show you how to generate XML-formatted data for third-party calls using PHP and how to get XML data from third parties.

Generating XML Format data

We assume that there is a student information sheet student in the system that needs to be provided to third party calls, and that there are id,name,sex,age to record the student's name, gender, age, and other information.

CREATE TABLE ' student ' (' ID ' int (one) not null auto_increment, ' name ' varchar () ' = ' not null, ' sex ' varchar (ten) NOT NULL, ' a GE ' smallint (3) Not NULL default ' 0 ', PRIMARY KEY (' id ')) engine=myisam default Charset=utf8;

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

Include_once ("connect.php"); Connect 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 ']  ;}

At this point, the data is stored in the $arr, and you can use Print_r to print the data test.
Next, the XML is created, the array is looped, and the data is written to the corresponding node in the XML.

$doc = new DOMDocument (' 1.0 ', ' utf-8 '); Declaration 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 have called PHP's built-in class DOMDocument to process and generate XML. For the resulting XML format, click here to see the results

<?xml version= "1.0" encoding= "Utf-8"?> Li Wanghao
 
   
  
     
   
    
   
      
   
    
     
    male
   
       ...
 
  

Get XML Format data

Now we assume that we want 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 to the local database. The key step here is to parse the XML.

PHP has a number of ways to parse XML, where PHP provides a built-in XmlReader class that can sequentially browse through the nodes of an XML file, and you can imagine a cursor passing through the node of the entire document and grabbing what is needed. Using XmlReader is efficient, especially for reading very large XML data, and using XmlReader consumes very little memory relative to other methods.

Header ("content-type:text/html; Charset=utf-8 "); $url =" http://www.helloweba.com/demo/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 the $i%3 to determine the modulo, because in the obtained XML, the information under node data is in the presence of 3 child nodes.

More interested in PHP related content readers can view the site: "PHP operation skills Summary FOR XML files", "PHP date and Time usage summary", "PHP Object-oriented Programming tutorial", "PHP string (String) Usage Summary", "php+ MySQL database operation Getting Started tutorial and PHP Common database operation Skills Summary

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • PHP generates JSON and XML type interface data formats
    • How to add CDATA tags when PHP generates XML
    • PHP generates sitemap.xml map functions
    • PHP Backup Database code (generate Word,excel,json,xml,sql)
    • 3 ways to generate XML files in PHP speed efficiency comparison
    • 4 ways to generate XML files in PHP share
    • PHP generates XML simple instance code
    • PHP get local picture file and generate XML file output specific ideas
    • PHP gets the XML data through the HTTP protocol post and parses the XML.
    • PHP generated XML with Flash get as garbled ultimate solution

http://www.bkjia.com/PHPjc/1106117.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106117.html techarticle PHP generates and obtains XML format data, PHP gets the XML format This article describes how PHP generates and obtains XML format data. Share to everyone for your reference, as follows: in ...

  • 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.