PHP XML and JSON data generation interface

Source: Internet
Author: User
Tags cdata

<span style= "FONT-SIZE:18PX;" >/** * return result set * * @param mixed valid data set returned by $info or error description * @param string $msg is empty or error type code * @par Am string $result a successful or failed identity * @param int $post 1 is XML, 2 is JSON * */function getData ($info, $m sg = ", $post, $result = ' success ') {/* is divided into XML and JSON two ways */$data _arr = array (' result ' = = $result, ' msg ' = = $msg, '    Info ' = $info);  $data _arr = To_utf8_iconv ($data _arr); Ensure that the encoding passed is UTF-8 if ($post = = 1) {/* XML way */if (class_exists (' DOMDocument ')) {$do            C=new DOMDocument (' 1.0 ', ' UTF-8 ');            $doc->formatoutput=true;            $shopex = $doc->createelement (' Kewin ');            $doc->appendchild ($shopex);            $result = $doc->createelement (' result ');            $shopex->appendchild ($result);            $result->appendchild ($doc->createcdatasection ($data _arr[' result '));            $msg = $doc->createelement (' msg '); $shopex-> AppendChild ($msg);            $msg->appendchild ($doc->createcdatasection ($data _arr[' msg "));            $info = $doc->createelement (' info ');            $shopex->appendchild ($info);            Create_tree ($doc, $info, $data _arr[' info ');        Die ($doc->savexml ());    } die (' <?xml version= ' 1.0 "encoding=" UTF-8 "?>". Array2xml ($data _arr));        } else {/* JSON mode */$json = new json;    Die ($json->encode ($data _arr));            Print out the generated return string}}/** * Loop Generation XML node * * @param handle $doc XML instance handle * @param handle $top  Current parent node * @param array $info _arr required to parse * @param boolean $have _item is an array of data, you need to add the item parent node on each piece of data * */function Create_tree ($doc, $top, $info _arr, $have _item = False) {if (Is_array ($info _arr)) {foreach ($in                Fo_arr as $key = = $val) {if (Is_array ($val)) {if ($have _item = = False)             {       $data _info= $doc->createelement (' Data_info ');                    $top->appendchild ($data _info);                Create_tree ($doc, $data _info, $val, true);                    } else {$item = $doc->createelement (' item ');                    $top->appendchild ($item);                    $key _code = $doc->createattribute (' key ');                    $item->appendchild ($key _code);                    $key _code->appendchild ($doc->createtextnode ($key));                Create_tree ($doc, $item, $val);                }} else {$text _code= $doc->createelement ($key);                $top->appendchild ($text _code);                if (is_string ($val)) {$text _code->appendchild ($doc->createcdatasection ($val)); } else {$text _code->appendchild ($doc->createtextnode ($vaL));    }}}}} else {$top->appendchild ($doc->createcdatasection ($info _arr)); }}function Array2xml ($data, $root = ' Shopex ') {$xml = ' < '. $root. '    > ';    _array2xml ($data, $xml); $xml. = ' </'. $root. '    > '; return $xml;} Function _array2xml (& $data,& $xml) {if (Is_array ($data)) {foreach ($data as $k + = $v) {if (is_nu Meric ($k)) {$xml. = ' <item key= "'. $k.                ' > ';                $xml. =_array2xml ($v, $xml);            $xml. = ' </item> ';                }else{$xml. = ' < ' $k. ' > ';                $xml. =_array2xml ($v, $xml);            $xml. = ' </'. $k. ' > ';    }}}elseif (Is_numeric ($data)) {$xml. = $data; }elseif (is_string ($data)) {$xml. = ' <![ Cdata['. $data. ']    > '; }}</span>

Call Method GetData () pass in different parameters to generate XML data or JSON-formatted data, respectively

GetData ($data, ', ' json '); Return JSON data

{"Result": "Success", "MSG": "", "info": {"Data_info": [{"goods_id": "1", "last_modify": "1423937979"},{"goods_id": "2", "Last_modify": "1425595831"},{"goods_id": "3", "last_modify": "1423937959"},{"goods_id": "4", "Last_modify": " 1423942862 "}]," Counts ":" 4 "}}

GetData ($data, ', ' xml '); return XML data

<?xml version= "1.0" encoding= "UTF-8"?>
<kewin>
<result><! [cdata[success]]></result>
<msg><! [cdata[]]></msg>
<info>
<data_info>
<item key= "0" >
<goods_id><! [cdata[1]]></goods_id>
<last_modify><! [cdata[1423937979]]></last_modify>
</item>
<item key= "1" >
<goods_id><! [cdata[2]]></goods_id>
<last_modify><! [cdata[1425595831]]></last_modify>
</item>
<item key= "2" >
<goods_id><! [cdata[3]]></goods_id>
<last_modify><! [cdata[1423937959]]></last_modify>
</item>
<item key= "3" >
<goods_id><! [cdata[4]]></goods_id>
<last_modify><! [cdata[1423942862]]></last_modify>
</item>
</data_info>
<counts><! [cdata[4]]></counts>
</info>
</kewin>


PHP XML and JSON data generation interface

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.