PHP returns XML data

Source: Internet
Author: User

When it comes to XML format data in development, how does PHP return data in XML format?

Create a new class file, called arraytoxml.php

<?PHP/** * Array to XML class * Use method: Call the Parse method in the class, pass in the array and the node name*/classArraytoxml {//Document Object    Private Static $doc=NULL; //Version number    Private Static $version= ' 1.0 '; /** * Initialize document version and encoding * * @param string $version version number * @param string $encoding XML encoding*/     Public Static functionInit$version,$encoding) { self::$doc=NewDomDocument ($version,$encoding); Self::$doc->formatoutput =true; }          /** * Convert array to XML * @param array $array to be converted * @param string $rootName node name * @param string $versio N Version number * @param string $encoding XML encoding * @return string*/     Public Static functionParse$array,$rootName= ' Root ',$version= ' 1.0 ',$encoding= ' UTF-8 ') { self:: Init ($version,$encoding); //Conversion        $node= Self::convert ($array,$rootName); Self::$doc->appendchild ($node); returnSelf::$doc-SaveXML (); }          /** * Recursive conversion * @param array $array arrays * @param string $nodeName node name * @return Object (domelement) */    Private Static functionConvert$array,$nodeName) {        if(!Is_array($array))return false; //Create parent Node        $node= Self::createnode ($nodeName); //Loop Array        foreach($array  as $key=$value) {            $element= Self::createnode ($key); //if it is not an array, the value of the node is created            if(!Is_array($value)) {               $element->appendchild (Self::createvalue ($value)); $node->appendchild ($element); } Else {                //if it is an array, the recursion                $node->appendchild (Self::convert ($value,$key,$element)); }                               }        return $node; }Private Static functionCreateNode ($name) {        $node=NULL; //if it is a string, the node is created        if(!Is_numeric($name)) {            $node= Self::$doc->createelement ($name); } Else {            //if it is a number, the default item node is created            $node= Self::$doc->createelement (' item ')); }        return $node; }          /** * Create text node * @param string | | bool | | integer $value * @return object (Domtext | |     Domcdatasection); */    Private Static functionCreateValue ($value) {        $textNode=NULL; //if it is a bool type, it is converted to a string        if(true===$value||false===$value) {            $textNode= Self::$doc->createtextnode ($value? ' True ': ' false '); } Else {            //Create CDATA Nodes if HTML tags are included            if(Strpos($value, ' < ') >-1) {                $textNode= Self::$doc->createcdatasection ($value); } Else {                $textNode= Self::$doc->createtextnode ($value); }        }        return $textNode; }}?>

Then create a new index.php file:

<?PHPinclude(' arraytoxmls.php '); $data=Array(' Cehi_one ' =Array(' Title1 ' and ' Test title One ', ' content1 ' = ' test content one '), ' ceshi_two ' =Array(' Title2 ' = ' Test title two ', ' content2 ' = ' test content two ', ' link ' = ' http://www.cnblogs.com/kekeer '), ' ceshi_three ' =Array(Array(' Title3 ' = ' Test title two ', ' content3 ' = ' test content two ', ' link ' = ' http://www..com/'),Array(' Title2 ' = ' Test two ', ' content2 ' = ' test content two ', ' link ' = ' http://www.cnblogs.com/kekeer '))) ; $api=NewArraytoxml (); $rs=$api->parse ($data); Echo $rs;?>

Run index.php and see the source code to see the effect.

Note: class file thanks to the original author for sharing.

PHP returns XML data

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.