使用php进行JSON TO XML转换 [英] JSON TO XML convertion with php

查看:279
本文介绍了使用php进行JSON TO XML转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此数据转换为xml时仍然坚持..

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
        )

)





这是我想要的xml格式的json数据..





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>





但这是我在转换json数据时得到的内容.. 。



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>







这是使用php的脚本....






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");
		}
	}
}



有谁可以帮我解决问题?无论是从javascript还是php,我都会感激。

谢谢。


Anybody who can help me fix the problem? either from the side of javascript or php i will appreciate.
Thanks.

推荐答案

templateData =
templateData =


_POST [' data'];

// 初始化或创建数组
_POST['data']; // initializing or creating array


template_info =
template_info =


这篇关于使用php进行JSON TO XML转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆