DreamsCoder.com

We Code your Dreams


home > XML to JSON conversion using PHP




XML to JSON conversion using PHP







Click here for live demo



Source code



$xmlNode = simplexml_load_file($url);
$arrayData = xmlToArray($xmlNode);
function xmlToArray($xml, $options = array()) 
{
   $defaults = array('namespaceSeparator' =>':',
//Seperator
 'attributePrefix' => '@',   
//to distinguish between attributes and nodes 
with the same name
 'alwaysArray' => array(),
 'autoArray' => true,        
 'textContent' => '$',       
 'autoText' => true,
 'keySearch' => false,       
 'keyReplace' => false       
    );
    $options = array_merge($defaults, $options);
    $namespaces = $xml->getDocNamespaces();
    $namespaces[''] = null; 
 
    //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;
echo "
"; $attributesArray[$attributeKey] = (string)$attribute; } } $tagsArray = array(); foreach ($namespaces as $prefix => $namespace) { foreach ($xml->children($namespace) as $childXml) { $childArray = xmlToArray($childXml, $options); list($childTagName, $childProperties) = each($childArray); if ($options['keySearch']) $childTagName = str_replace($options['keySearch'], $options['keyReplace'], $childTagName); if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName; if (!isset($tagsArray[$childTagName])) { $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)) { $tagsArray[$childTagName][] = $childProperties; } else { $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 ); } $jdata = json_encode($arrayData);

Label - php





by - Chinmay Mahajan





Are you looking for something else?


Suggested Programs




How to Integrate Google reCAPTCHA in Your Website


Get distance between two cities using PHP


How to plot route between two points on google map using PHP


Tracking visitors location


How to download File using PHP code