function array_xml($array, $num_prefix = "num_") { if (!is_array($array)) { return $array; } else { $return = null; foreach ($array as $key => $val) { $key = is_numeric($key) ? $num_prefix : $key; $return .= "<" . $key . ">" . array_xml($val, $num_prefix) . "</" . $key . ">"; } } return $return; }
/** * 返回结果集 * * @param mixed $info 返回的有效数据集或是错误说明 * @param string $msg 为空或是错误类型代号 * @param string $result 请求成功或是失败的标识 * @param int $post 1为xml方式,2为json方式 * */ function data_back($info, $msg = '', $post, $result = 'success') { /* 分为xml和json两种方式 */ $data_arr = array('result' => $result, 'msg' => $msg, 'info' => $info); $data_arr = to_utf8_iconv($data_arr); //确保传递的编码为UTF-8 if ($post == 1) { /* xml方式 */ if (class_exists('DOMDocument')) { $doc = new DOMDocument('1.0', 'UTF-8'); $doc->formatOutput = true; $shopex = $doc->createElement('shopex'); $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"?>' . array_xml($data_arr)); } else { /* json方式 */ $json = new JSON(); die($json->encode($data_arr)); //把生成的返回字符串打印出来 } }
/** * CSV to XML with tags by CSV_HEAD. */ function csv2xmlByHead($fileIn, $valPreserve = FALSE, $gambi = false) { $CSV_HEAD = $out = ''; $fconv = function ($tmp) use(&$out, $valPreserve, $gambi) { global $CSV_HEAD; if ($gambi) { $tmp[5] = localValido($tmp[5]); } $out .= "\n<tr>" . array_xml($CSV_HEAD, $tmp, $valPreserve) . "</tr>"; }; if (csv_get($fileIn, '', $fconv)) { return "<table>\n{$out}\n</table>\n"; } else { die("\nERRO ao abrir arquivo '{$fileIn}'\n"); } }
function on_response() { global $out_buffer; global $errno, $aborted; if ($aborted) { return; } $responses = array(); $responses[CONFIG\RESULT_KEY] = 0; foreach ($out_buffer as $key => $opts) { $value = process_out_opts($key, $opts); $responses[$key] = $value; } flush_db(); switch (CONFIG\OUT_FORM) { case "json": echo json_encode($responses); break; case "xml": header('Content-type: text/xml'); echo "<?xml version='1.0' encoding='UTF-8'?>\n"; echo array_xml($responses); break; } }