Search for:
Jump to: 
Your Ad Here
Post new topic Reply to topic
Author Message
 Post subject: To Convert xml data to jsonp (crossdomain) for highcharts
PostPosted: Tue Feb 19, 2013 6:05 am 
Offline

Joined: Thu Jan 17, 2013 5:05 am
Posts: 4
Hi frineds ,
Bydefault the highcharts are not supported crossdomain feeds , so if you want to pass data from cross domain and that is in xml format, then we convert data into jsonp format, see the example code below

//xmltojson.php
<?php
function xmlToArray($xml, $options = array()) {
$defaults = array(
'namespaceSeparator' => ':',//you may want this to be something other than a colon
'attributePrefix' => '', //to distinguish between attributes and nodes with the same name
'alwaysArray' => array(), //array of xml tag names which should always become arrays
'autoArray' => true, //only create arrays for tags which appear more than once
'textContent' => '$', //key used for the text content of elements
'autoText' => true, //skip textContent key if node has no attributes or child nodes
'keySearch' => false, //optional search and replace on tag and attribute names
'keyReplace' => false //replace values for above search values (as passed to str_replace())
);
$options = array_merge($defaults, $options);
$namespaces = $xml->getDocNamespaces();
$namespaces[''] = null; //add base (empty) namespace

//get attributes from all namespaces
$attributesArray = array();
foreach ($namespaces as $prefix => $namespace) {
foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
//replace characters in attribute name
if ($options['keySearch']) $attributeName =
str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
$attributeKey = $options['attributePrefix']
. ($prefix ? $prefix . $options['namespaceSeparator'] : '')
. $attributeName;
$attributesArray[$attributeKey] = (string)$attribute;
}
}

//get child nodes from all namespaces
$tagsArray = array();
foreach ($namespaces as $prefix => $namespace) {
foreach ($xml->children($namespace) as $childXml) {
//recurse into child nodes
$childArray = xmlToArray($childXml, $options);
list($childTagName, $childProperties) = each($childArray);

//replace characters in tag name
if ($options['keySearch']) $childTagName =
str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
//add namespace prefix, if any
if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;

if (!isset($tagsArray[$childTagName])) {
//only entry with this key
//test if tags of this type should always be arrays, no matter the element count
$tagsArray[$childTagName] =
in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']
? array($childProperties) : $childProperties;
} elseif (
is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])
=== range(0, count($tagsArray[$childTagName]) - 1)
) {
//key already exists and is integer indexed array
$tagsArray[$childTagName][] = $childProperties;
} else {
//key exists so convert to integer indexed array with previous value in position 0
$tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
}
}
}

//get text content of node
$textContentArray = array();
$plainText = trim((string)$xml);
if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;

//stick it all together
$propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')
? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;

//return node as array
return array(
$xml->getName() => $propertiesArray
);
}

?>


//jsonp.php
<?php
header("content-type: application/json");
include('xmltojson.php');

$xmlNode = simplexml_load_file('https://www.igolder.com/GoldData.ashx?type=Historical&months=91¤cy=USD&tz=UTC&unit=oz&output=xml');

$arrayData = xmlToArray($xmlNode);
$data1 = json_encode($arrayData);
$data = json_decode($data1);
date_default_timezone_set('UTC');
foreach ($data->GoldQuotes->Price as $message) {

$res[] = array(strtotime($message->Date)* 1000,$message->Value);
$message->Date;
$message->Value;
}

sort($res);

foreach ($res as $key => $val) {

$res2 = $_GET['callback']. '('. json_encode($res) . ')';
}
echo str_replace(array('"'), "", $res2);

?>

I think it's useful , feel free to ask any doubts.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic    [ 1 post ] 

1, 2, 3, 4, 5 ... 413
Recent topics  Replies   Views   Last post 
No new posts Jquery ajax Pagination using php

by rajkumar » Tue Apr 19, 2011 11:14 am in Coding Repository

9

17730

Tue May 28, 2013 5:19 am

vfdg View the latest post

No new posts How to Convert DVD and Video to Your iphone 4,iphone 4 to pc

by smsbackuper » Wed May 18, 2011 8:11 am in PHP-Code Help

2

1544

Thu May 23, 2013 6:39 am

greenwoove View the latest post

No new posts How to upgrade and migrate Joomal site

by mallikharjuna rao » Mon Jan 21, 2013 1:40 pm in Joomla

1

397

Thu May 16, 2013 9:53 am

sunitha12 View the latest post

No new posts Benefits of PHP

by hirephpexpert » Fri Sep 07, 2012 4:41 am in CakePHP

6

1700

Wed May 15, 2013 6:25 am

PrettyPrincesG View the latest post

No new posts How to stop user from getting registered in Joomla

by mallikharjuna rao » Tue Jan 22, 2013 5:16 am in Joomla

3

1759

Wed May 15, 2013 6:17 am

PrettyPrincesG View the latest post


Your Ad Here
cron