Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am stuck to while converting this data to xml..
JavaScript
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
<?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
<?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 ....

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

I have updated the function array_to_xml. Hope it will solve your problem:
PHP
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: ;
            }
        }
 
Share this answer
 
@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
XML
<themes> <theme> .... </theme> <theme> </theme> </themes>
<packages> <packagedata> .... </packagedata> <packagedata> </packagedata> </packages>


This is what i used....
PHP
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;
		}
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900