laravel json到xml

laravel json到xml,json,xml,laravel-4,Json,Xml,Laravel 4,我需要使用laravel将json格式转换为xml。 我期望输出如下 <MESSAGE> <AUTHKEY>Your auth key</AUTHKEY> <SENDER>SenderID</SENDER> <ROUTE>Template</ROUTE> <CAMPAIGN>campaign name</CAMPAIGN> <COUNTRY>

我需要使用laravel将json格式转换为xml。 我期望输出如下

   <MESSAGE>
    <AUTHKEY>Your auth key</AUTHKEY>
   <SENDER>SenderID</SENDER>
   <ROUTE>Template</ROUTE>
   <CAMPAIGN>campaign name</CAMPAIGN>
   <COUNTRY>country code</COUNTRY>
   <SMS TEXT="message1">
   <ADDRESS TO="number1"></ADDRESS>
   </SMS>
   </MESSAGE>
此xml将json输出为xml:

<?xml version="1.0" encoding="utf-8"?>
<xml>
<MESSAGE>
    <AUTHKEY>68252AGguI2SK45395d8f7</AUTHKEY>
    <ROUTE>4</ROUTE>
    <CAMPAIGN>TEST</CAMPAIGN>
    <SENDER>OODOOP</SENDER>
    <SMS>
        <TEXT>HI MANI</TEXT>
        <ADDRESS><-TO>8870300358
        </-TO>
    </ADDRESS>
</SMS>
</MESSAGE>undefined</xml>

68252AGguI2SK45395d8f7
4.
试验
哦
嗨,玛尼
8870300358
未定义

我得到了错误的输出,标记错误。

我知道这是一个旧线程,但我希望这可以帮助任何寻求支持的人

<?php

//build and array of the data you want to writ as XML
$xml_array = array ("message" => array(
        "authkey"=> "68252AGguI2SK45395d8f7",
        "route" => 4,
        "campaign" => "test",
        "sender" => "oooop",
        "sms" => array (
            "attribute" => "text:Hi Man",
            "address" =>array (
                        "attribute" => "to:8870300358"
                    )
            )

        )
    );


// create an XML object 
$xmlobj = new SimpleXMLElement("<root></root>");

// a convert function 
// this will take array in the above format and convert it in to XML Object 
function convert($array, $xml){
    foreach($array  as $key=>$line){
        if(!is_array($line)){
            $data = $xml->addChild($key, $line);

        }else{
            $obj = $xml->addChild($key);

            if(!empty($line['attribute'])){

                $attr = explode(":",$line['attribute']);
                $obj->addAttribute($attr[0],$attr[1]);
                unset($line['attribute']);
            }
            convert($line, $obj);
        }
    }
    return $xml;
}
$xmldoc =convert($xml_array,$xmlobj);
// text.xml is a blank file to write the data in it , you should create it first and give it WRITE permission
// in my case i just create it by and 
print $xmldoc->asXML('test.xml');
print "<a href='test.xml'> XML file</a>";
数组(
“authkey”=>“68252AGguI2SK45395d8f7”,
“路线”=>4,
“活动”=>“测试”,
“发件人”=>“oooop”,
“sms”=>数组(
“属性”=>“文本:Hi-Man”,
“地址”=>数组(
“属性”=>“到:8870300358”
)
)
)
);
//创建一个XML对象
$xmlobj=新的SimpleXMLElement(“”);
//转换函数
//这将采用上述格式的数组,并将其转换为XML对象
函数转换($array,$xml){
foreach($key=>$line的数组){
如果(!是_数组($line)){
$data=$xml->addChild($key,$line);
}否则{
$obj=$xml->addChild($key);
如果(!empty($line['attribute'])){
$attr=explode(“:”,$line['attribute']);
$obj->addAttribute($attr[0],$attr[1]);
未设置($line['attribute']);
}
转换($line$obj);
}
}
返回$xml;
}
$xmldoc=convert($xml_数组,$xmlobj);
//xml是一个空白文件,要在其中写入数据,应首先创建它并授予其写入权限
//在我的例子中,我只是通过和创建它
打印$xmldoc->asXML('test.xml');
打印“”;

:)

您使用什么函数将json转换为xml?
<?php

//build and array of the data you want to writ as XML
$xml_array = array ("message" => array(
        "authkey"=> "68252AGguI2SK45395d8f7",
        "route" => 4,
        "campaign" => "test",
        "sender" => "oooop",
        "sms" => array (
            "attribute" => "text:Hi Man",
            "address" =>array (
                        "attribute" => "to:8870300358"
                    )
            )

        )
    );


// create an XML object 
$xmlobj = new SimpleXMLElement("<root></root>");

// a convert function 
// this will take array in the above format and convert it in to XML Object 
function convert($array, $xml){
    foreach($array  as $key=>$line){
        if(!is_array($line)){
            $data = $xml->addChild($key, $line);

        }else{
            $obj = $xml->addChild($key);

            if(!empty($line['attribute'])){

                $attr = explode(":",$line['attribute']);
                $obj->addAttribute($attr[0],$attr[1]);
                unset($line['attribute']);
            }
            convert($line, $obj);
        }
    }
    return $xml;
}
$xmldoc =convert($xml_array,$xmlobj);
// text.xml is a blank file to write the data in it , you should create it first and give it WRITE permission
// in my case i just create it by and 
print $xmldoc->asXML('test.xml');
print "<a href='test.xml'> XML file</a>";