/** * Description: * Convert a JSON object into XML format * * @param $a - JSON object * @return - XML string representation of the JSON object */ public function jsonToXML($a) { $a = json_decode($a, TRUE); // Decode the JSON into an associative array return arrayToXML($a); // Convert that array into an XML string }
function arrayToXML($array, $passedViaEntry, $allowedContent) { $xmlString = ""; foreach (array_keys($array) as $arrayKey) { // For each array item $isEntry = false; // Define as an entry $linkType = $arrayKey; // Set the arrayKey to the linkType if (is_int($linkType)) { // If a linkType is an INT, then we are manipulating an entry $linkType = "entry"; $isEntry = true; // Set as an entry } $arrayContent = $array[$arrayKey]; // Get the value of this particular array item if (is_array($arrayContent)) { // If the array item content is an array, meaning there is multiple of this type if ($isEntry == false) { // If this is NOT an entry foreach ($arrayContent as $thisTagAttributes) { // For each arrayIndex in the attributesArray $tagContent = ""; if (array_key_exists("@attributes", $thisTagAttributes)) { // If there is an @attributes in thisTagAttributes $thisTagAttributes = $thisTagAttributes["@attributes"]; // Re-assign the tag attributes to this particular @attributes } foreach ($thisTagAttributes as $attribute => $attributeValue) { // For each tag attribute $tagContent = $tagContent . $attribute . '="' . $attributeValue . '" '; } $xmlString = $xmlString . "\n\t<" . $linkType . " " . $tagContent . "/>"; } } else { // If it IS an entry if (filterContent($arrayContent["title"], $allowedContent) !== true) { // If we are NOT going to be filtering this content $tagContent = arrayToXML($arrayContent, true, $allowedContent); // Recursively create the XML for the entry array content $xmlString = $xmlString . "\n<" . $linkType . ">" . $tagContent . "\n</" . $linkType . ">"; } } } else { // If it is NOT an array if ($passedViaEntry == true) { // If we are doing a recursive inner entry parsing $tabChar = "\t"; // Allow the tab character so we can indent the tag } else { $tabChar = ""; // Disallow the tab character } $xmlString = $xmlString . "\n" . $tabChar . "<" . $linkType . ">" . $array[$arrayKey] . "</" . $linkType . ">"; } } return $xmlString; }
/** * Array to XML */ function arrayToXML(&$array) { foreach ($array as $key => $value) { $keyName = is_numeric($key) ? 'elm' . $key : $key; if (!is_array($value)) { $ret .= sprintf('<%1$s>%2$s</%1$s>', $keyName, $value) . LF; } else { $ret .= sprintf('<%1$s>', $keyName) . LF; $ret .= arrayToXML(&$value); $ret .= sprintf('</%1$s>', $keyName) . LF; } } return $ret; }
function arrayToXML(array $array, SimpleXMLElement &$xml) { foreach ($array as $key => $value) { // Array if (is_array($value)) { $xmlChild = is_numeric($key) ? $xml->addChild("id_{$key}") : $xml->addChild($key); arrayToXML($value, $xmlChild); continue; } // Object if (is_object($value)) { $xmlChild = $xml->addChild(get_class($value)); arrayToXML(get_object_vars($value), $xmlChild); continue; } // Simple Data Element is_numeric($key) ? $xml->addChild("id_{$key}", $value) : $xml->addChild($key, $value); } }
function testArrayToXML() { $array = array('something' => array('one' => 1, 'two' => array('too' => 2))); $xml = '<?xml version="1.0" encoding="UTF-8"?><something><one>1</one><two><too>2</too></two></something>'; $this->assertEqual($xml, arrayToXML($array)); }
function arrayToXML($Arraydata) { $xml = ''; if (isset($Arraydata[0])) { foreach ($Arraydata as $key => $value) { $xml .= arrayToXML($value); } } else { if (isset($Arraydata['attribute']) && is_array($Arraydata['attribute'])) { $xml .= "<" . $Arraydata['name'] . ' '; foreach ($Arraydata['attribute'] as $akey => $attr) { $xml .= $akey . '="' . htmlspecialchars($attr) . '" '; } } else { $xml .= "<" . $Arraydata['name']; } if (isset($Arraydata['value'])) { $xml .= '>' . @htmlspecialchars($Arraydata['value']) . '</' . $Arraydata['name'] . '>'; } else { if (isset($Arraydata['item']) && is_array($Arraydata['item']) && $Arraydata['item']) { $xml .= '>'; $xml .= arrayToXML($Arraydata['item']); $xml .= @htmlspecialchars("") . '</' . $Arraydata['name'] . '>'; } else { $xml .= '/>'; } } } return $xml; }
function printServiceData($arrData, $format = null, $statusCode = 200) { if ($statusCode == null || !is_numeric($statusCode)) { $statusCode = 200; } $envelop = getMsgEnvelop(); if ($format == null) { $format = $_REQUEST['format']; } if (getConfig("SERVICE_SHOW_REQUEST")) { $arrData['Request']['uri'] = SiteLocation . $GLOBALS['LOGIKS']["_SERVER"]['REQUEST_URI']; $arrData['Request']['site'] = $_REQUEST['site']; $arrData['Request']['scmd'] = $_REQUEST['scmd']; $arrData['Request']['format'] = $format; if (isset($GLOBALS['LOGIKS']["_SERVER"]["REQUEST_TIME_FLOAT"])) { $arrData['Request']['latency'] = microtime(true) - $GLOBALS['LOGIKS']["_SERVER"]["REQUEST_TIME_FLOAT"]; } else { $arrData['Request']['latency'] = microtime(true) - $GLOBALS['LOGIKS']["_SERVER"]["REQUEST_SERVICE_START"]; } $arrData['Request']['slug'] = array(); foreach ($_REQUEST['slug'] as $key => $value) { $arrData['Request']['slug']["SLUG_{$key}"] = $value; } } $htmlFormats = array("list", "select", "table"); if (in_array($format, $htmlFormats)) { if (isset($_REQUEST['debug']) && $_REQUEST['debug'] == "true") { header("Content-Type:text/text"); } else { header("Content-Type:text/html"); } } else { header("Content-Type:text/{$format}"); } if (getConfig("SERVICE_SHOW_ERROR_CODE")) { header("Status: {$statusCode}"); //header(':', true, $statusCode); header("HTTP/1.1 {$statusCode}"); } $msgData = $arrData['Data']; // $msgData=array( // // "a"=>"m", // // "c"=>"n", // // "a","b", // // "a"=>array("x"=>array("m"=>"n"),"z"=>"w"), // // "b"=>array("m"=>"n","o"=>"p"), // // array("x"=>array("m"=>"n"),"z"=>"w"), // // array("m"=>"n","o"=>"p"), // ); switch ($format) { case 'table': if (is_array($msgData)) { if (isset($_REQUEST['autoformat']) && $_REQUEST['autoformat'] == "false") { printFormattedArray($msgData, false, "table"); } else { printFormattedArray($msgData, true, "table"); } } else { echo "<tr><td>{$msgData}</td></tr>"; } break; case 'list': if (is_array($msgData)) { if (isset($_REQUEST['autoformat']) && $_REQUEST['autoformat'] == "false") { printFormattedArray($msgData, false, "list"); } else { printFormattedArray($msgData, true, "list"); } } else { echo "<li>{$msgData}</li>"; } break; case 'select': if (is_array($msgData)) { if (isset($_REQUEST['autoformat']) && $_REQUEST['autoformat'] == "false") { printFormattedArray($msgData, false, "select"); } else { printFormattedArray($msgData, true, "select"); } } else { echo "<option>{$msgData}</option>"; } break; case 'xml': $xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><service></service>"); arrayToXML($arrData, $xml); //array_walk_recursive($arrData, array ($xml, 'addChild')); echo $xml->asXML(); break; case 'json': echo json_encode($arrData); break; case 'txt': if (is_array($msgData)) { trigger_logikserror(900, E_USER_ERROR); } else { $msgData = strip_tags($msgData); echo $msgData; } default: //Anything else (raw,css,js) if (is_array($msgData)) { printFormattedArray($msgData); } else { $msgData = strip_tags($msgData); echo $msgData; } break; } }
function sendToAPI($url, $request = 'GET', $xml = array()) { if ($request != "GET") { if (substr_count($url, '?') > 0) { $url = str_replace('?', '.' . FORMAT . '?', $url); } else { $url .= '.' . FORMAT; } } else { if (substr_count($url, '?') > 0) { $url = str_replace('?', '.xml?', $url); } else { $url .= '.xml'; } } $xml = sizeof($xml) > 0 ? arrayToXML($xml) : false; $ch = new miniCURL(); $data = $ch->send($url, $request, $xml); return $ch->loadString($data); }
function sendToAPI($url, $request = 'GET', $xml = array()) { if ($request != "GET") { if (substr_count($url, '?') > 0) { $url = str_replace('?', '.' . FORMAT . '?', $url); } else { $url .= '.' . FORMAT; } } else { if (substr_count($url, '?') > 0) { $url = str_replace('?', '.xml?', $url); } else { $url .= '.xml'; } } //Problem here: DELETE doesn't seem to be working now. //arrayToXML is making the opening xml tag if nothing else, so the isempty seems to fail if (!empty($xml)) { $xml = arrayToXML($xml); } else { $xml = false; } //Not sure about this isEmpty is it even necessary? if (isEmpty($xml)) { $xml = false; } $ch = new miniCURL(); $data = $ch->send($url, $request, $xml); return $ch->loadString($data); }
if (!$matches) { rest_utils::sendResponse($data, 400); } } $id = $data->getRequestVars(); if (isset($id['id'])) { $done = newObj('model_' . $modelId)->getEntity($id['id']); } else { $done = newObj('model_' . $modelId)->getAll(); } // stop if model entity not found if (!$done) { rest_utils::sendResponse($data, 500, $done); } if ($http_accept == 'xml') { rest_utils::sendResponse($data, 200, arrayToXML($done, $modelId), 'application/xml'); } elseif ($http_accept == 'atom') { ob_start(); require LIBRARY . '/view/atom.phtml'; $out = ob_get_clean(); rest_utils::sendResponse($data, 200, $out, 'application/atom+xml'); } else { rest_utils::sendResponse($data, 200, json_encode($done), 'application/json'); } break; case 'post': //rest_utils::authenticate(); $in = newObj('entity_' . $modelId)->validate($data->getRequestVars(), sprintf('madr_%s_%s', $data->getMethod(), $modelId)); if (!$in) { rest_utils::sendResponse($data, 400); }
function arrayToXML($arr, &$xml_node) { foreach ($arr as $key => $value) { if (is_array($value)) { if (!is_numeric($key)) { $subnode = $xml_node->addChild("{$key}"); arrayToXML($value, $subnode); } else { arrayToXML($value, $xml_node); } } else { $xml_node->addChild("{$key}", "{$value}"); } } }
function arrayToXML($array_in) { $return = ""; $attributes = array(); foreach ($array_in as $k => $v) { if ($k[0] == "@") { // attribute... $attributes[str_replace("@", "", $k)] = $v; } else { if (is_array($v)) { $return .= generateXML($k, arrayToXML($v), $attributes); $attributes = array(); } else { if (is_bool($v)) { $return .= generateXML($k, $v == true ? "true" : "false", $attributes); $attributes = array(); } else { $return .= generateXML($k, $v, $attributes); $attributes = array(); } } } } return $return; }