1.PHP Generating XML data
① Stitching Strings
② using System classes (Domdocument,xmlwriter,simplexml)
Example 1 uses the DomDocument class in the PHP system class:
<? PHP $dom New DomDocument (' 1.0 ', ' utf-8 '); $element $dom->createelement (' Test ', ' this is a root element '); $dom->appendchild ($element); Echo $dom->savexml ();
Page output
This is a root element
View Source code display:
<? XML version= "1.0" encoding= "Utf-8" ?> < Test > This is a root element</test>
Example 2 stitching a string
//Modify HTTP header informationHeader("Content-type:text/xml");//XML Header Information$xml= "<?xml version= ' 1.0 ' encoding= ' utf-8 '? >\n";//root node start tag$xml. = "<root>\n";//Code$xml. = "<code>200</code>\n"; //message$xml. = "<message> Data return success </message>\n"; //Data$xml. = "<data>\n"; $xml. = "<id>1</id>\n";$xml. = "<name>john</name>\n";$xml. = "</data>\n";//root node end tag$xml. = "</root>";Echo $xml;Exit();
Page output:
This XML file does does appear to has any style information associated with it. The document tree is shown below.<Root><Code>200</Code><message>Data returned successfully</message><Data><ID>1</ID><name>John</name></Data></Root>
HTTP Response header information:
2.XML-Way encapsulated communication interface
<?PHPclassresponse{/** * Output communication data as XML * @param integer $code Status code * @param string $message message * @param array $data data * return string*/ Public Static functionxml$code,$message,$data){ if(!Is_numeric($code)){ return‘‘; } $result=Array( ' Code ' =$code, ' message ' =$message, ' data ' =$data ); //Modify HTTP header information Header("Content-type:text/xml"); //XML Header Information $xml= "<?xml version= ' 1.0 ' encoding= ' utf-8 '?>"; //root node start tag $xml. = "<root>"; $xml. = Self::xmltoencode ($result); //root node end tag $xml. = "</root>"; Echo $xml; Exit(); } //parsing $result to XML Public Static functionXmltoencode ($data){ $xml=$attr= ""; foreach($data as $k=$v){ //If $k is a number (the data in the Code,message,data also contains an indexed array), make the following judgment if(Is_numeric($k)){ $attr= "Id= ' {$k}‘"; $k= ' Item '; } $xml. = "<{$k} {$attr}> "; //if $v is an array, the method is called recursively if(Is_array($v)){ $xml. = Self::xmltoencode ($v); }Else{ $xml.=$v; } $xml. = "</{$k}> "; } return $xml; }}
Call this page test.php
$data the first case:
<? PHP require ' response.php '; $data Array ( ' id ' =>1, ' name ' = ' Mary '); Response:: XML (200, ' data return succeeded ',$data);
Page output:
file does not appear to has any style information associated with it. The document tree is shown below.<root><code>200</code><message> data return success </message>< Data><id>1</id><name>mary</name></data></root>
$data the second case
<? PHP require ' response.php '; $data Array ( ' id ' =>1, ' name ' = ' Mary ', ' type ' = +array//<0>1 </0><1>3</1><2>6</2> = <item id= "0" >1</item> ... ); Response:: XML (200, ' data return succeeded ',$data);
Page output:
file does not appear to has any style information associated with it. The document tree is shown below.<root><code>200</code><message> data return success </message>< Data><id>1</id><name>mary</name><type><item id= "0" >1</item>< Item id= "1" >3</item><item id= "2" >6</item></type></data></root>
$data the third situation:
<? PHP require ' response.php '; $data Array ( ' id ' =>1, ' name ' = ' Mary ', ' type ' = +array(' A ' =>1, ' B ' =>3, ' C ' =>6 )); Response:: XML (200, ' data return succeeded ',$data);
Page output:
file does not appear to has any style information associated with it. The document tree is shown below.<root><code>200</code><message> data return success </message>< Data><id>1</id><name>mary</name><type><a>1</a><b>3</b ><c>6</c></type></data></root>
PHP Development APP Interface Summary-XML encapsulation of communication interfaces