function respond($status, $DATA) { /* PRINTING THE XML FORMATTED DATA */ $xml = new SimpleXMLElement('<XML/>'); $xml->addChild('STATUS', $status); arr2xml($DATA, $xml); print $xml->asXML(); }
function respond($status, $DATA, $EXIT = false) { /* PRINTING THE XML FORMATTED DATA */ $xml = new SimpleXMLElement('<XML/>'); $xml->addChild('STATUS', $status); arr2xml($DATA, $xml); print_r($xml); //print $xml->asXML(); if ($EXIT == true) { exit; } }
function loopSourceDir($sourceFiles) { $transunitTemplate = file_get_contents(TARGET_SUB_TEMPLATE); $transTemplate = file_get_contents(TARGET_TEMPLATE); foreach ($sourceFiles as $sourceFile) { if (!is_file(SOURCE_DIR . '/' . $sourceFile)) { continue; } $sourceFileExtension = pathinfo(SOURCE_DIR . '/' . $sourceFile, PATHINFO_EXTENSION); if ($sourceFileExtension != SOURCE_EXTENSION) { continue; } $arr = csv2arr($sourceFile, SOURCE_DELIMITER); arr2xml($arr, $sourceFile, $transunitTemplate, $transTemplate); } }
function respond($status, $DATA) { /* NAME: respond - XML Response Handler * DESC: Creates and prints a XML document with a STATUS tag and a * associative data array converted into XML format. * AUTHOR: Martin Thomsen * USAGE: respond($status, $DATA); */ $xml = new SimpleXMLElement('<XML/>'); // CREATE XML OBJECT $xml->addChild('STATUS', $status); // ADD STATUS TO XML if ($DATA != '') { arr2xml($DATA, $xml); } // ADD DATA ARRAY TO XML exit($xml->asXML()); // PRINT XML AND EXIT }
function arr2xml($arr, $node = null) { if ($node === null) { $simxml = new simpleXMLElement('<?xml version="1.0" encoding="utf-8"?><root></root>'); } else { $simxml = $node; } //simpleXMLElement对象如何添加子节点 foreach ($arr as $k => $v) { if (is_array($v)) { arr2xml($v, $simxml->addChild($k)); } else { if (is_numeric($k)) { $simxml->addChild('item', $v); } else { $simxml->addChild($k, $v); } } } return $simxml->savexml(); }
/** * Function: arr2xml * Recursively adds an array (or object I guess) to a SimpleXML object. * * Parameters: * &$object - The SimpleXML object to modify. * $data - The data to add to the SimpleXML object. */ function arr2xml(&$object, $data) { foreach ($data as $key => $val) { if (is_int($key) and (empty($val) or is_string($val) and trim($val) == "")) { unset($data[$key]); continue; } if (is_array($val)) { if (in_array(0, array_keys($val))) { # Numeric-indexed things need to be added as duplicates foreach ($val as $dup) { $xml = $object->addChild($key); arr2xml($xml, $dup); } } else { $xml = $object->addChild($key); arr2xml($xml, $val); } } else { $object->addChild($key, fix($val, false, false)); } } }
/** * 将数组转换为xml * @param array $data 要转换的数组 * @param bool $root 是否要根节点 * @return string xml字符串 */ public function arr2xml($data, $root = true) { $str = ""; if ($root) { $str .= "<xml>"; } foreach ($data as $key => $val) { if (is_array($val)) { $child = arr2xml($val, false); $str .= "<{$key}>{$child}</{$key}>"; } else { $str .= "<{$key}><![CDATA[{$val}]]></{$key}>"; } } if ($root) { $str .= "</xml>"; } return $str; }
/** * Function: make_posts_xml * Updates all of the post XML data to well-formed non-CDATAized XML. * * Versions: 1.1.3.2 => 2.0 */ function make_posts_safe() { if (!($posts = SQL::current()->query("SELECT * FROM __posts"))) { return; } if (!SQL::current()->query("SELECT xml FROM __posts")) { return; } function clean_xml(&$input) { $input = trim($input); } while ($post = $posts->fetchObject()) { if (!substr_count($post->xml, "<![CDATA[")) { continue; } $post->xml = str_replace("<![CDATA[]]>", "", $post->xml); $xml = simplexml_load_string($post->xml, "SimpleXMLElement", LIBXML_NOCDATA); $parse = xml2arr($xml); array_walk_recursive($parse, "clean_xml"); $new_xml = new SimpleXMLElement("<post></post>"); arr2xml($new_xml, $parse); echo _f("Sanitizing XML data of post #%d...", array($post->id)) . test(SQL::current()->update("posts", array("id" => $post->id), array("xml" => $new_xml->asXML()))); } }
/** * 建立请求,以表单HTML形式构造(默认) * @param $arr 要转换的数组 * @param $xml xml对象 */ function arr2xml($arr, $xml) { foreach ($arr as $k => $v) { if (is_array($v)) { $x = $xml->addChild($k); arr2xml($v, $x); } else { $xml->addChild($k, $v); } } }
function respond($status, $DATA, $EXIT = false, $template = false, $human = false) { /* Tools Paths */ //global $CGE; $domain = ''; $toolspath = '/tools_new/client/platform'; if ($template == true) { // Load the CGE Class :: ARGUMENTS=($title, $meta, $banner,$css,$js) $CGE = new CGE('CGE Server', '<base href="' . $domain . '">', '/images/cge_buttons/banner.gif', '', ''); // CGE MENU $CGE->std_header("CGE Server", "({$toolspath}/user_settings.php,'User Home'),(/services/,'Services'),({$toolspath}/isolate_manager.php,'Sample Overview'),({$toolspath}/map.php,'Map')"); // Print the Menu } if ($human == true) { /* PRINTING THE HUMAN RESPONSE */ // REQUIRE THE USER TO LOGIN if (isset($CGE)) { if ($CGE->user_is_logged_in()) { // Write Message echo "<br><table style='margin:auto;'><tr><th>Server Response</th><th>Message</th></tr><tr><td style='text-align:center;'>{$status}</td><td>" . str_replace("\n", "<br>", $DATA['MESSAGE']) . "</td></tr></table>"; } } else { echo "<br><table style='margin:auto;'><tr><th>Server Response</th><th>Message</th></tr><tr><td style='text-align:center;'>{$status}</td><td>" . str_replace("\n", "<br>", $DATA['MESSAGE']) . "</td></tr></table>"; } } else { /* PRINTING THE XML RESPONSE */ $xml = new SimpleXMLElement('<XML/>'); $xml->addChild('STATUS', $status); arr2xml($DATA, $xml); print $xml->asXML(); } if ($template == true) { // TRACK USER TRAFIC $CGE->Piwik(14); # STANDARD FOOTER $CGE->standard_foot("Support", "('Technical problems','CGE Support','cgehelp')"); } if ($EXIT == true) { exit; } }