function array_to_xml($phpResponse, &$phpResponseToXML) { foreach ($phpResponse as $key => $value) { if (is_array($value)) { if (!is_numeric($key)) { // For non-numeric keys, give name same as key to the node $subnode = $phpResponseToXML->addChild("{$key}"); // Recursive call to the function since the value was an array array_to_xml($value, $subnode); } else { // For numeric keys, give a name to the node //$subnode = $phpResponseToXML->addChild("item$key"); if (current($phpResponseToXML->xpath('parent::*'))) { $subnode = $phpResponseToXML->addChild("Showing"); } else { $subnode = $phpResponseToXML->addChild("Show"); } //Recursive call to the function since the value was an array array_to_xml($value, $subnode); } } else { // Save the node and its value in XMLFiles format //$phpResponseToXML->addChild("$key","$value"); $phpResponseToXML->{$key} = $value; } } }
/** * 数组转XML */ function array_to_xml($arr) { $xml = ''; if (!empty($arr)) { foreach ($arr as $key => $value) { if (is_array($value)) { if (array_key_exists(0, $value)) { foreach ($value as $kk => $vv) { $xml .= '<' . $key . ' id="' . $key . '_' . $kk . '">' . "\n"; $xml .= array_to_xml($vv); $xml .= '</' . $key . '>' . "\n"; } } else { if (!is_numeric($key)) { $xml .= '<' . $key . '>' . "\n"; } $xml .= array_to_xml($value); if (!is_numeric($key)) { $xml .= '</' . $key . '>' . "\n"; } } } else { $xml .= '<' . $key . '>' . $value . '</' . $key . '>' . "\n"; } } } return $xml; }
private function array_to_xml($array, $container = 'response', $is_root = true) { if (!is_array($array)) { return array_to_xml(array($array)); } $xml = ''; if ($is_root) { $xml .= '<?xml version="1.0" encoding="utf-8"?>'; $xml .= "<{$container}>"; } foreach ($array as $key => $value) { // make sure key is a string $elem = $key; if (!is_string($key) && !empty($container)) { $elem = $container; } $xml .= "<{$elem}>"; if (is_array($value)) { if (array_keys($value) !== array_keys(array_keys($value))) { $xml .= array_to_xml($value, '', false); } else { $xml .= array_to_xml($value, r('/s$/', '', $elem), false); } } else { $xml .= htmlspecialchars($value, ENT_COMPAT, 'ISO-8859-1') != $value ? "<![CDATA[{$value}]]>" : $value; } $xml .= "</{$elem}>"; } if ($is_root) { $xml .= "</{$container}>"; } return preg_replace('/[\\x00-\\x1F\\x7F]/', '', $xml); }
/** * as_nprml(): Translates a post to NPRML. Returns an XML string. */ function as_nprml($post) { $story = post_to_nprml_story($post); $doc = array(); $doc[] = array('tag' => 'list', 'children' => array(array('tag' => 'story', 'children' => $story))); $ret_xml = array_to_xml('nprml', array('version' => '0.93'), $doc); return $ret_xml; }
function smarty_function_xml($params, &$smarty) { // initializing or creating array $player_info = $params[toXml]; // creating object of SimpleXMLElement $xml_player_info = new SimpleXMLElement("<?xml version=\"1.0\"?><player_info></player_info>"); // function call to convert array to xml array_to_xml($player_info, $xml_player_info); //print return $xml_player_info->asXML(); }
function wsResponse($format, $status, $return) { $response = array("status" => $status, "response" => $return); if (strtolower($format) == 'json') { print json_encode($response); } else { if (strtolower($format) == 'xml') { print array_to_xml($response, new SimpleXMLElement('<root />'))->asXML(); } } }
function array_to_xml($data, &$xml_data) { foreach ($data as $key => $value) { if (is_array($value)) { if (is_numeric($key)) { $key = 'item' . $key; } $subnode = $xml_data->addChild($key); array_to_xml($value, $subnode); } else { $xml_data->addChild("item", htmlspecialchars("{$value}")); } } }
function get_comments_xml($filename, $cons) { global $wpdb; $sql = "SELECT post_title,comment_ID,comment_author, comment_date_gmt, comment_content\n\t\t\tFROM {$wpdb->comments}\n\t\t\t\tLEFT OUTER JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID)\n\t\t\t\tINNER JOIN {$wpdb->term_relationships} as r1 ON ({$wpdb->posts}.ID = r1.object_id)\n\t\t\t\tINNER JOIN {$wpdb->term_taxonomy} as t1 ON (r1.term_taxonomy_id = t1.term_taxonomy_id)\n\t\t\tWHERE comment_approved = '1'\n\t\t\t\tAND comment_type = ''\n\t\t\t\tAND post_password = ''\n\t\t\t\tAND t1.taxonomy = 'category'\n\t\t\t\tAND t1.term_id = " . $cons . "\n\t\t\torder by comment_date_gmt"; $data = $wpdb->get_results($sql, ARRAY_A); // creating object of SimpleXMLElement $xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>'); // function call to convert array to xml array_to_xml($data, $xml_data); //saving generated xml file; $result = $xml_data->asXML(); echo $result; exit; }
function array_to_xml(array $arr, SimpleXMLElement $xml) { try { foreach ($arr as $k => $v) { $kk = $k; if (is_numeric($k)) { $kk = 'i-' . $k; } is_array($v) ? array_to_xml($v, $xml->addChild($kk)) : $xml->addChild($kk, str_replace('&', '&', $v)); } } catch (Exception $e) { } return $xml; }
public function export_small($array, $level = 1) { $xml = ''; foreach ($array as $key => $value) { $key = strtolower($key); if (is_object($value)) { $value = get_object_vars($value); } // convert object to array if (is_array($value)) { $multi_tags = false; foreach ($value as $key2 => $value2) { if (is_object($value2)) { $value2 = get_object_vars($value2); } // convert object to array if (is_array($value2)) { $xml .= str_repeat("\t", $level) . "<{$key}>\n"; $xml .= array_to_xml($value2, $level + 1); $xml .= str_repeat("\t", $level) . "</{$key}>\n"; $multi_tags = true; } else { if (trim($value2) != '') { if (htmlspecialchars($value2) != $value2) { $xml .= str_repeat("\t", $level) . "<{$key2}><![CDATA[{$value2}]]>" . "</{$key2}>\n"; } else { $xml .= str_repeat("\t", $level) . "<{$key2}>{$value2}</{$key2}>\n"; // changed $key to $key2 } } $multi_tags = true; } } if (!$multi_tags and count($value) > 0) { $xml .= str_repeat("\t", $level) . "<{$key}>\n"; $xml .= array_to_xml($value, $level + 1); $xml .= str_repeat("\t", $level) . "</{$key}>\n"; } } else { if (trim($value) != '') { if (htmlspecialchars($value) != $value) { $xml .= str_repeat("\t", $level) . "<{$key}>" . "<![CDATA[{$value}]]></{$key}>\n"; } else { $xml .= str_repeat("\t", $level) . "<{$key}>{$value}</{$key}>\n"; } } } } return $xml; }
function array_to_xml($data, &$xml_data) { foreach ($data as $key => $value) { if (is_array($value)) { if (is_numeric($key)) { $key = 'LineItem'; //dealing with <0/>..<n/> issues } $subnode = $xml_data->addChild($key); array_to_xml($value, $subnode); } else { $xml_data->addChild("{$key}", htmlspecialchars("{$value}")); } } }
function array_to_xml($arr_in, &$xml_out) { foreach ($arr_in as $key => $value) { if (is_array($value)) { if (!is_numeric($key)) { $subnode = $xml_out->addChild("{$key}"); array_to_xml($value, $subnode); } else { array_to_xml($value, $xml_out); } } else { $xml_out->addChild("{$key}", "{$value}"); } } }
public function arrayToXML($request) { $xml_data = new \SimpleXMLElement('<Request></Request>'); foreach ($request as $key => $value) { if (is_array($value)) { if (is_numeric($key)) { $key = 'item' . $key; } $subnode = $xml_data->addChild($key); array_to_xml($value, $subnode); } else { $xml_data->addChild("$key", htmlspecialchars("$value")); } } return $xml_data; }
function my_xmlapi_output($array, $print = true) { if (isset($array['success']) && is_bool($array['success'])) { $array['success'] = $array['success'] == true ? 1 : 0; } // creating object of SimpleXMLElement $xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root></root>"); // function call to convert array to xml array_to_xml($array, $xml); if ($print) { header('Content-Type: text/xml'); print $xml->asXML(); die; } return $xml->asXML(); }
function array_to_xml($student_info, &$xml_student_info) { foreach ($student_info as $key => $value) { if (is_array($value)) { if (!is_numeric($key)) { $subnode = $xml_student_info->addChild("{$key}"); array_to_xml($value, $subnode); } else { $subnode = $xml_student_info->addChild("item{$key}"); array_to_xml($value, $subnode); } } else { $xml_student_info->addChild("{$key}", htmlspecialchars("{$value}")); } } }
public static function xml($array, $level = 1) { $xml = ''; if ($level == 1) { $xml .= '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n<array>\n"; } foreach ($array as $key => $value) { $key = strtolower($key); if (is_array($value)) { $multi_tags = false; foreach ($value as $key2 => $value2) { if (is_array($value2)) { $xml .= str_repeat("\t", $level) . "<{$key}>\n"; $xml .= array_to_xml($value2, $level + 1); $xml .= str_repeat("\t", $level) . "</{$key}>\n"; $multi_tags = true; } else { if (trim($value2) != '') { if (htmlsafe($value2) != $value2) { $xml .= str_repeat("\t", $level) . "<{$key}><![CDATA[{$value2}]]>" . "</{$key}>\n"; } else { $xml .= str_repeat("\t", $level) . "<{$key}>{$value2}</{$key}>\n"; } } $multi_tags = true; } } if (!$multi_tags and count($value) > 0) { $xml .= str_repeat("\t", $level) . "<{$key}>\n"; $xml .= array_to_xml($value, $level + 1); $xml .= str_repeat("\t", $level) . "</{$key}>\n"; } } else { if (trim($value) != '') { if (htmlsafe($value) != $value) { $xml .= str_repeat("\t", $level) . "<{$key}>" . "<![CDATA[{$value}]]></{$key}>\n"; } else { $xml .= str_repeat("\t", $level) . "<{$key}>{$value}</{$key}>\n"; } } } } if ($level == 1) { $xml .= "</array>\n"; } return $xml; }
function cb_to_xml_pymnts($json) { $decode = json_decode($json); $content = $decode->content; $inv = $content->invoice; $inv_payments = $inv->linked_transactions; foreach ($inv_payments as $inv_payment) { $payment['Invoice']['InvoiceNumber'] = "CB-INV-" . $inv->id; $payment['Account']['Code'] = $_GET['pay_id']; $payment['Date'] = date("Y-m-d\\TH:i:s", $inv_payment->txn_date); $payment['Amount'] = $inv_payment->txn_amount / 100; $payments[] = $payment; } $xml_data = new SimpleXMLElement('<?xml version="1.0"?><Payments></Payments>'); array_to_xml($payments, $xml_data); return $xml_data->asXML(); }
function array_to_xml($array, &$xml_user_info) { foreach ($array as $key => $value) { if (is_array($value)) { if (!is_numeric($key)) { $subnode = $xml_user_info->addChild("{$key}"); array_to_xml($value, $subnode); } else { //$key += 1; $subnode = $xml_user_info->addChild("Day"); array_to_xml($value, $subnode); } } else { //$key += 1; $xml_user_info->addChild("Place", htmlspecialchars("{$value}")); } } }
/** * Output ajax * * @param mixed $data The data to encode and output. * @param string $output_type The output encode type. Options 'json', 'xml', 'html', or 'text'. Defaults to 'json'. * @param boolean $ajax_only Reject if this is not called by an xmlhttprequest. Defaults to TRUE. */ function output_ajax($data = NULL, $output_type = 'json', $require_ajax = TRUE) { $_output = NULL; if ($require_ajax === TRUE && !IS_AJAX) { $this->CI->output->set_status_header('403'); $this->CI->output->set_header("Content-Type: text/plain"); $_output = 'Invalid Request Origin'; } else { // Encode data and set headers switch ($output_type) { case 'json': default: $_output = json_encode($data); $this->CI->output->set_header('Content-Type: application/json'); break; case 'xml': $this->CI->load->helper('encode_xml'); $_output = array_to_xml($data); $this->CI->output->set_header('Content-Type: application/xhtml+xml'); break; case 'text': $_output = $data; $this->CI->output->set_header('Content-Type: text/plain'); break; case 'html': $_output = $data; $this->CI->output->set_header('Content-Type: text/html'); break; } $this->CI->output->set_status_header('200'); $this->CI->output->set_header('Cache-Control: no-store, no-cache, must-revalidate'); $this->CI->output->set_header('Pragma: no-cache'); $this->CI->output->set_header('Access-Control-Allow-Origin: ' . base_url()); $this->CI->output->set_header('Content-Length: ' . strlen($_output)); } $this->CI->output->set_output($_output); }
// Neu $obj->insertvon = $uid; $obj->insertamum = date('Y-m-d H:i:s'); } if (!$error) { // Attribute zuweisen zum Speichern foreach ($savedata as $key => $value) { $obj->{$key} = $value; } } } $return = ''; if (!$error && ($return = call_user_func_array(array($obj, $method), $parameter))) { $data['result'] = $obj->cleanResult(); $data['return'] = $return; $data['error'] = 'false'; $data['errormsg'] = ''; } else { $data['result'] = ''; $data['return'] = $return; $data['error'] = 'true'; $data['errormsg'] = $obj->errormsg; } // Daten ausgeben if ($typ == 'json') { echo json_encode($data); } elseif ($typ == 'xml') { echo array_to_xml($data); } else { var_dump($data); }
function array_to_xml($array, $level = 1) { if (is_array($array)) { $xml = ''; if ($level == 1) { $xml .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n<thinkedit>\n"; } foreach ($array as $key => $value) { $key = strtolower($key); if (is_array($value)) { $multi_tags = false; foreach ($value as $key2 => $value2) { if (is_array($value2)) { $xml .= str_repeat("\t", $level) . "<{$key}>\n"; $xml .= array_to_xml($value2, $level + 1); $xml .= str_repeat("\t", $level) . "</{$key}>\n"; $multi_tags = true; } else { if (trim($value2) != '') { if (htmlspecialchars($value2) != $value2) { $xml .= str_repeat("\t", $level) . "<{$key}><![CDATA[{$value2}]]>" . "</{$key}>\n"; } else { $xml .= str_repeat("\t", $level) . "<{$key}>{$value2}</{$key}>\n"; } } else { $xml .= str_repeat("\t", $level) . "<{$key}>0</{$key}>\n"; } $multi_tags = true; } } if (!$multi_tags and count($value) > 0) { $xml .= str_repeat("\t", $level) . "<{$key}>\n"; $xml .= array_to_xml($value, $level + 1); $xml .= str_repeat("\t", $level) . "</{$key}>\n"; } } else { if (trim($value) != '') { if (htmlspecialchars($value) != $value) { $xml .= str_repeat("\t", $level) . "<{$key}>" . "<![CDATA[{$value}]]></{$key}>\n"; } else { $xml .= str_repeat("\t", $level) . "<{$key}>{$value}</{$key}>\n"; } } else { $xml .= str_repeat("\t", $level) . "<{$key}>0</{$key}>\n"; } } } if ($level == 1) { $xml .= "</thinkedit>\n"; } return $xml; } else { return false; } }
/** * Convert an multi-dimensional array to xml. * http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml * * @param object $object Link to SimpleXMLElement object * @param array $data Array which need to convert into xml */ function array_to_xml(array $data, SimpleXMLElement $object) { foreach ($data as $key => $value) { if (is_array($value)) { if (is_array_assoc($value)) { // For associative arrays use keys as child object $new_object = $object->addChild($key); array_to_xml($value, $new_object); } else { // For sequential arrays use parent key as child foreach ($value as $new_value) { if (is_array($new_value)) { array_to_xml(array($key => $new_value), $object); } else { $object->addChild($key, $new_value); } } } } else { //$object->$key = $value; // See note about & here - http://php.net/manual/en/simplexmlelement.addchild.php#112204 $object->addChild($key, $value); } } }
} } // Keyword tags $keywordTagsResult = mysql_query("select Keyword, InfoKeywordID\nfrom StoryInfoKeywordTag natural join InfoKeyword\nwhere StoryID = {$storyID} order by Keyword", $db_conn); $story['keywordCount'] = mysql_num_rows($keywordTagsResult); if (mysql_num_rows($keywordTagsResult) > 0) { $story['keywords'] = array(); while ($row = mysql_fetch_array($keywordTagsResult)) { $tag = array(); $tag['id'] = $row['InfoKeywordID']; $tag['name'] = htmlspecialchars($row['Keyword']); $story['keywords'][] = $tag; } } // Location tags $locationTagsResult = mysql_query("select StoryID, TagID, Longitude, Latitude\nfrom StoryLocationTag\nwhere StoryID = {$storyID}", $db_conn); $story['locationCount'] = mysql_num_rows($locationTagsResult); if (mysql_num_rows($locationTagsResult) > 0) { $story['locations'] = array(); while ($row = mysql_fetch_array($locationTagsResult)) { $tag = array(); $tag['id'] = $row['TagID']; $tag['latitude'] = $row['Latitude']; $tag['longitude'] = $row['Longitude']; $story['locations'][] = $tag; } } //Print $xmlStory = array_to_xml($story, new SimpleXMLElement('<story/>'))->asXML(); echo $xmlStory; include 'close_db.php';
function array_to_xml($data) { $r = ''; foreach ($data as $k => $v) { if (is_array($v)) { $r .= "<{$k}>" . array_to_xml($v) . "</{$k}>"; } elseif (is_string($v)) { $r .= "<{$k}><![CDATA[{$v}]]></{$k}>"; } else { $r .= "<{$k}>{$v}</{$k}>"; } } return $r; }
$node_info['icon'] = $node->getIcon(); // get allowed items if ($allowed_items = $node->getAllowedItems()) { foreach ($allowed_items as $allowed_item) { $node_info['allowed_items'] = $allowed_item['type']; } } // get children if ($children = $node->getChildren()) { foreach ($children as $child) { $child_info['title'] = $child->getTitle(); $child_info['icon'] = $child->getIcon(); $child_info['id'] = $child->getId(); $node_info['children'][] = $child_info; } } $out['node'][] = $node_info; $out['result'] = true; $out['message'] = translate('api_node_info_done'); } else { $out['result'] = false; $out['message'] = translate('api_node_not_found'); } } else { $out['message'] = translate('api_unknown_action requested'); $out['result'] = false; } /************************** output results ***********************/ header("Content-Type: text/xml"); echo array_to_xml($out);
/** * Admin! * */ function adminGenerator() { if (($table = get_array_value($this->CONFIG, 'table', false, '~^[a-zA-Z_][a-zA-Z_0-9]*$~')) == false) { popup_message_add('[ NEWS ] table not defined or configuration error', JCMS_MESSAGE_ERROR); return false; } // get all news $query = CMS::$DB->query("select stamp, id, caption, link, page, streams, summary from `{$table}`"); if ($query == false) { popup_message_add('Query error: ' . get_array_value(CMS::$DB->errorInfo(), 2), JCMS_MESSAGE_ERROR); return false; } // format all items at a time into XML and then transform to HTML $xml = array_to_xml($query->fetchAll(PDO::FETCH_ASSOC), array('all-news-list', 'news')); return XSLTransform($xml->saveXML($xml->documentElement), __DIR__ . '/list.xsl'); }
// $Id: options_url.php,v 1.1 2011-01-20 14:36:25 arenou Exp $ //Gestion des options de type text $base_path = "../.."; $base_auth = "CATALOGAGE_AUTH|ADMINISTRATION_AUTH"; include $base_path . "/includes/init.inc.php"; require_once "{$include_path}/parser.inc.php"; require_once "{$include_path}/fields_empr.inc.php"; $options = stripslashes($options); //Si enregistrer if ($first == 1) { $param["FOR"] = "url"; $param[SIZE][0][value] = stripslashes($SIZE * 1); $param[MAXSIZE][0][value] = stripslashes($MAXSIZE * 1); $param[TIMEOUT][0][value] = stripslashes($TIMEOUT * 1); $param[REPEATABLE][0][value] = $REPEATABLE ? 1 : 0; $options = array_to_xml($param, "OPTIONS"); ?> <script> opener.document.formulaire.<?php echo $name; ?> _options.value="<?php echo str_replace("\n", "\\n", addslashes($options)); ?> "; opener.document.formulaire.<?php echo $name; ?> _for.value="url"; self.close();
/** * @dataProvider providerArrayToXML * @group templates */ public function testArrayToXML($array, $result) { $xml = new SimpleXMLElement('<template/>'); array_to_xml($array, $xml); $this->assertSame($result, $xml->asXML()); }
/** * 数据XML编码 * @param mixed $data 数据 * @param string $item 数字索引时的节点名称 * @param string $id 数字索引key转换为的属性名 * @return string */ function array_to_xml($data, $item = 'item', $id = 'id') { $xml = $attr = ''; foreach ($data as $key => $val) { if (is_numeric($key)) { $id && ($attr = " {$id}=\"{$key}\""); $key = $item; } $xml .= "<{$key}{$attr}>"; $xml .= is_array($val) || is_object($val) ? array_to_xml($val, $item, $id) : $val; $xml .= "</{$key}>"; } return $xml; }
$story['locationCount'] = $row['LocationCount']; $story['entityCount'] = $row['EntityCount']; $story['categoryCount'] = $row['CategoryCount']; if (strlen($row['Categories']) > 0) { $story['categories'] = array(); $categoryIDTags = explode(',', $row['Categories']); foreach ($categoryIDTags as $id) { $tag = array(); $tag['id'] = $id; $story['categories'][] = $tag; } } if (strlen($row['GeoTags']) > 0) { $story['locations'] = array(); $geotags = explode(';', $row['GeoTags']); foreach ($geotags as $gt) { $longlat = explode(',', $gt); $tag = array(); $tag['longitude'] = $longlat[0]; $tag['latitude'] = $longlat[1]; $story['locations'][] = $tag; } } $stories[] = $story; } $xmlResult = array(); $xmlResult['storiesCount'] = sizeof($stories); $xmlResult['stories'] = $stories; //Print echo array_to_xml($xmlResult, new SimpleXMLElement('<xmlResult/>'))->asXML(); include 'close_db.php';