Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I am stuck to while converting this data to xml..
 
Array
(
    [metadata] => Array
        (
            [name] => template name
            [description] => template description
            [version] => 1.0.0
            [author] => 1
        )
 
    [themes] => Array
        (
            [theme] => Array
                (
                    [0] => Array
                        (
                            [name] => default
                            [filename] => default
                            [isDefault] => 0
                        )
 
                    [1] => Array
                        (
                            [name] => blue
                            [filename] => blue
                            [isDefault] => 1
                        )
 
                )
 
        )
 
    [packages] => Array
        (
            [0] => 2
            [1] => 1
        )
 
)
 
This is what i want from the json data in xml format..
 
<?xml version="1.0"?>
<template>
	<metadata>
		<name>template name</name>
		<description>template description</description>
		<version>1.0.0</version>
		<author>1</author>
	</metadata>
	<themes>
		<theme>
			<name>default</name>
			<filename>default</filename>
			<isDefault>0</isDefault>
		</theme>
		<theme>
			<name>blue</name>
			<filename>blue</filename>
			<isDefault>1</isDefault>
		</theme>
	</themes>
	
</template>
 
 
But this is what i am getting when i convert the json data...
<?xml version="1.0"?>
<template>
	<metadata>
		<name>template name</name>
		<description>template description</description>
		<version>1.0.0</version>
		<author>1</author>
	</metadata>
	<themes>
		<theme>
			<name>default</name>
			<filename>default</filename>
			<isDefault>0</isDefault>
		
			<name>blue</name>
			<filename>blue</filename>
			<isDefault>1</isDefault>
		</theme>
	</themes>
	
</template>
 
 

This is the script iam using with php ....
 
$templateData =  $_POST['data'];
 
// initializing or creating array
$template_info =  $templateData;
 
// creating object of SimpleXMLElement
$xml_template_info = new SimpleXMLElement("<?xml version=\"1.0\"?><template></template>");
 
// function call to convert array to xml
array_to_xml($template_info,$xml_template_info);
 
//saving generated xml file
 $xml_template_info->asXML(dirname(__FILE__)."/manifest.xml") ;
 
// function defination to convert array to xml
function array_to_xml($template_info, &$xml_template_info) {
	foreach($template_info as $key => $value) {
		if(is_array($value)) {
			if(!is_numeric($key)){
				$subnode = $xml_template_info->addChild("$key");
				array_to_xml($value, $subnode);
			}
			else{
				array_to_xml($value, $xml_template_info);
			}
		}
		else {
			$xml_template_info->addChild("$key","$value");
		}
	}
}
Anybody who can help me fix the problem? either from the side of javascript or php i will appreciate.
Thanks.
Posted 26-Feb-13 4:37am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I have updated the function array_to_xml. Hope it will solve your problem:
function array_to_xml($template_info, &$xml_template_info) {
            foreach($template_info as $key => $value) {
                if(is_array($value)) {
                    if(!is_numeric($key)){
 
                        $subnode = $xml_template_info->addChild("$key");
 
                        if(count($value) >1 && is_array($value)){
                            $jump = false;
                            $count = 1;
                            foreach($value as $k => $v) {
                                if(is_array($v)){
                                    if($count++ > 1)
                                        $subnode = $xml_template_info->addChild("$key");
 
                                    array_to_xml($v, $subnode);
                                    $jump = true;
                                }
                            }
                            if($jump) {
                                goto LE;
                            }
                            array_to_xml($value, $subnode);
                        }
                        else
                            array_to_xml($value, $subnode);
                    }
                    else{
                        array_to_xml($value, $xml_template_info);
                    }
                }
                else {
                    $xml_template_info->addChild("$key","$value");
                }
 
                LE: ;
            }
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

@Abdur...
Thanks for that comment... i used the same approach and i seem to be getting the correct data for the xml where i have to switch between the keys supplied....
 
i have data like
 
<themes> <theme> .... </theme> <theme> </theme> </themes>
<packages> <packagedata> .... </packagedata> <packagedata> </packagedata> </packages>
 
 
This is what i used....
 
if(!is_numeric($key)){
	echo "<pre>";
	
	switch( $key ){
		case "themes":
			$themesNode = $xml_template_info->addChild( "themes");	
		
			$themesValues = array_values( $value ) ;
				
			for ( $i = 0; $i< count( $themesValues[0] ) ; $i++ ){							
				$curKeys = array_keys($themesValues[0][$i] );
				$curValues = array_values($themesValues[0][$i] );
				
				$curNode = $themesNode->addChild( "theme");
				
				for( $j=0; $j< count( $curKeys ) ; $j++){
					$curNode->addChild( $curKeys[$j], $curValues[$j] );								
				}
			}
			break;
			
			
		case "pages":
			break;
		default:
			$subnode = $xml_template_info->addChild("$key");
			array_to_xml($value, $subnode);
			break;
		}
}
 
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 289
2 CPallini 275
3 DamithSL 260
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,422
2 Maciej Los 3,860
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,010


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100