Phpxml and json data generation APIs

Source: Internet
Author: User
Phpxml and json data generation APIs
/*** Return result set *** @ param mixed $ info returns a valid dataset or error description * @ param string $ msg is null or the error type code * @ param string $ result the request is successful. or the failed identifier * @ param int $ post 1 is in xml format, 2: json mode **/function getData ($ info, $ msg = '', $ post, $ result = 'success ') {/* is divided into xml and json methods */$ data_arr = array ('result' => $ result, 'MSG '=> $ msg, 'info' => $ info); $ data_arr = to_utf8_iconv ($ data_arr); // ensure that the passed encoding is UTF-8 if ($ post = 1) {/* xml method */if (class_exists ('domaindocument') {$ doc = 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 ('
 '. Array2xml ($ data_arr);} else {/* json mode */$ json = new JSON; die ($ json-> encode ($ data_arr )); // Print the generated returned string}/*** generate an xml node cyclically ** @ param handle $ doc xml instance handle * @ param handle $ top current parent node *@ param array $ info_arr array to be parsed * @ param boolean $ have_item is a data array, yes, you need to add the item parent node **/function create_tree ($ doc, $ top, $ info_arr, $ have_item = false) {if (is_array ($ info_arr )) {foreach ($ info_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. ='
 '; Return $ xml;} function _ array2xml (& $ data, & $ xml) {if (is_array ($ data )) {foreach ($ data as $ k =>$ v) {if (is_numeric ($ k) {$ xml. ='
 
  
'; $ Xml. = _ array2xml ($ v, $ xml); $ xml. ='
 ';} Else {$ xml. =' <'. $ k.'> '; $ xml. = _ array2xml ($ v, $ xml); $ xml. ='
 ';}} Elseif (is_numeric ($ data) {$ xml. = $ data;} elseif (is_string ($ data) {$ xml. =''.$data.'';}}

You can call getData () to input different parameters to generate xml data or json data.

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



success




1
1423937979


2
1425595831


3
1423937959


4
1423942862


4

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.