PHP generates XML data
1) Assembling strings
2) using System classes
-DomDocument
-XMLWriter
-SimpleXML
Here is an example of the first method:
$code, ' message ' and $message, ' Date ' and $data); echo Json_encode ($result); Exit } public static function XML () {header ("content-type:text/xml"); $xml = "
\ n "; $xml. = "
\ n "; $xml. = "
200 \ n"; $xml. = "
data returned successfully
\ n"; $xml. = "
\ n"; $xml. = "
1
\ n"; $xml. = "
Ceshi
\ n"; $xml. = "\ n"; $xml. = "
\ n "; Echo $xml; public static function Xmlencode ($code, $message, $data =array ()) {if (!is_numeric ($code)) {return; } $result = Array (' code ' = = $code, ' message ' + $message, ' data ' = $DATA); Header ("Content-type:text/xml"); $xml = "
"; $xml. = "
\ n "; $xml. = Self::xmltoencode ($result); echo $xml. = "
"; public static function Xmltoencode ($data) {$xml = $attr = '; foreach ($data as $key = + $value) {if (Is_numeric ($key)) {$attr = "id= ' {$key} '"; $key = ' item '; } $xml. = "<{$key} {$attr}>"; $xml. = Is_array ($value)? Self::xmltoencode ($value): $value; Recursively, if value is an array, the output node is recursive. $xml. = "
\ n "; } return $xml; }} $arr = array (' id ' = = 1, ' name ' = = ' xxx ', ' type ' = = Array (4,5,6), ' test ' = = Array (1,25,345=>a Rray (123, ' Zifuchuan '));//<0>4
4
Response::json (200, ' data return success ', $arr);//response::xml (); Response::xmlencode (+, ' success ', $arr);
The above describes the encapsulation of communication interface data method-xml, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.