Php 如何将jSON转换为XML
在我开始之前,我知道有很多类似的问题,但是相信我,我读了所有的,如果不是大部分的话。我尝试了一系列的解决方案,但似乎都不管用。我得到了一个空白的“树”作为我的结果。这是我正在使用的代码Php 如何将jSON转换为XML,php,xml,json,simplexml,Php,Xml,Json,Simplexml,在我开始之前,我知道有很多类似的问题,但是相信我,我读了所有的,如果不是大部分的话。我尝试了一系列的解决方案,但似乎都不管用。我得到了一个空白的“树”作为我的结果。这是我正在使用的代码 $jSON = json_decode('array here'); function array2xml($array, $xml = false){ if($xml === false){ $xml = new SimpleXMLElement('<result/>'); } fore
$jSON = json_decode('array here');
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<result/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
} else {
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
$jSON=jSON_decode('array here');
函数array2xml($array,$xml=false){
如果($xml==false){
$xml=新的SimpleXMLElement(“”);
}
foreach($key=>$value的数组){
if(是_数组($value)){
array2xml($value,$xml->addChild($key));
}否则{
$xml->addChild($key,$value);
}
}
返回$xml->asXML();
}
这是我正在使用的jSON数组
我不知道为什么它不起作用。下面是我使用该函数时的结果
<result>
<generated_in>155ms</generated_in>
</result>
155ms
不要为函数提供对象,而是尝试为数组提供对象:
$jSON = json_decode($raw_data, true);
// ^ add second parameter flag `true`
例如:
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<result/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild($key));
} else {
$xml->addChild($key, $value);
}
}
return $xml->asXML();
}
$raw_data = file_get_contents('http://pastebin.com/raw.php?i=pN3QwSHU');
$jSON = json_decode($raw_data, true);
$xml = array2xml($jSON, false);
echo '<pre>';
print_r($xml);
函数array2xml($array,$xml=false){
如果($xml==false){
$xml=新的SimpleXMLElement(“”);
}
foreach($key=>$value的数组){
if(是_数组($value)){
array2xml($value,$xml->addChild($key));
}否则{
$xml->addChild($key,$value);
}
}
返回$xml->asXML();
}
$raw\u data=file\u get\u contents('http://pastebin.com/raw.php?i=pN3QwSHU');
$jSON=jSON\u decode($raw\u data,true);
$xml=array2xml($jSON,false);
回声';
打印(xml);
这真的不难做到,我遇到了一个类似的问题,我只是通过执行以下操作解决了它,我想我会分享它,以防其他人尝试这样做。这件事做得很简单
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<result/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild(is_numeric((string) $key)?("n".$key):$key));
} else {
$xml->addChild(is_numeric((string) $key)?("n".$key):$key, $value);
}
}
return $xml->asXML();
}
受保护的功能显示($result){
if(空($this->output_type)|strtolower($this->output_type)==“json”){
标题('Content-type:application/json');
echo json_编码($result);
}else if(strtolower($this->output_type)=“xml”){
标题('Content-type:application/xml');
回声';
回声“;
$xml\u array=json\u decode(json\u encode($result),true);
$this->xmlWalker($xml_数组,“start”);
回声“;
}否则{///什么不可以
标题('Content-type:application/json');
echo json_编码($result);
}
}
受保护函数xmlWalker($xml_数组,$parent){
foreach($xml_数组作为$tag=>$value){
if((int)$tag===$tag){
$tag=mb_substr($parent,0,-1);
}
回声“;
if(是_数组($value)){
$this->xmlWalker($value,$tag);
}否则{
echo美元价值;
}
回声“;
}
}
我希望这有帮助我在使用Ghost解决方案时遇到了数字标记问题。我不得不对其进行一些更改以删除数字标记(之前只添加了一个n):
通过使用属性和同名节点,您可以获得更自然的xml输出。内置函数可以做到这一点
$xml=新的SimpleXMLElement(“”);
$arr=json_decode($your_数组,true);
array_walk_recursive($arr,array($xml,'addChild'));
是否有一种方法可以将它们格式化为树或节点?我尝试了标题('Content-type:text/xml')代码>但它看起来仍然相对相同。@Kreightive将它们格式化为树是什么意思?比如用标签正确格式化?我不认为你可以通过简单的xmlYes来美化它,确切地说是@Ghost。有什么方法可以做到吗?@Kreightive我不认为你可以在simplexml中做到这一点,但是你可以在domdocument中做到这一点,我认为这应该可以解决它。
function array2xml($array, $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<result/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, $xml->addChild(is_numeric((string) $key)?("n".$key):$key));
} else {
$xml->addChild(is_numeric((string) $key)?("n".$key):$key, $value);
}
}
return $xml->asXML();
}
function array2xml($array, $parentkey="", $xml = false){
if($xml === false){
$xml = new SimpleXMLElement('<result/>');
}
foreach($array as $key => $value){
if(is_array($value)){
array2xml($value, is_numeric((string) $key)?("n".$key):$key, $xml->addChild(is_numeric((string) $key)?$parentkey:$key));
} else {
$xml->addAttribute(is_numeric((string) $key)?("n".$key):$key, $value);
}
}
return $xml->asXML();
}
$xml = array2xml($jSON, "", false);
$xml = new SimpleXMLElement('<root/>');
$arr = json_decode($your_array, true);
array_walk_recursive($arr, array ($xml,'addChild'));