2014-11-17 5 views
3

시작하기 전에 이와 비슷한 질문이 있지만 대부분은 아니지만 모두 읽었습니다. 나는 여러 가지 솔루션을 시도했지만 그 중 아무 것도 작동하지 않는 것 같습니다. 나는 결과로 빈 "나무"를 얻고있다. 여기에 제가 사용하고있는 코드가 있습니다.jSON을 XML로 변환하는 방법

$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 배열이 있습니다.

http://pastebin.com/pN3QwSHU

나는 그것이 작동하지 않는 이유를 모르겠어요. 이 함수를 사용하면 결과가 나타납니다.

<result> 
<generated_in>155ms</generated_in> 
</result> 

답변

9

대신에 함수 객체를 먹이는 대신 배열을 공급하려고 :

$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); 

Sample Output

+1

내가 나무 나 노드로 포맷 할 수있는 방법이 있습니까? 나는'header ('Content-type : text/xml');을 시도했지만 상대적으로 여전히 유사합니다. – Criesval

+0

@Kreightive 나무로 형식화한다는 것은 무엇을 의미합니까? 제대로 탭 형식이 좋아? 나는 당신이 단순한 xml을 통해 그것을 prettify 수 있다고 생각하지 않습니다 – Ghost

+0

예, 정확히 @Ghost. 내가 할 수있는 방법이 있습니까? – Criesval

1

이 정말 너무 어려운 일이 아니다 하기 위해서, 나는 비슷한 문제가 있었고 다음과 같이함으로써 그것을 해결했다. 다른 누구도 그것을하려고했습니다. 매우 간단하게 처리됩니다.

protected function display($result) { 
     if (empty($this->output_type) || strtolower($this->output_type) == "json") { 
      header('Content-type: application/json'); 
      echo json_encode($result); 
     } else if (strtolower($this->output_type) == "xml") { 
      header('Content-type: application/xml'); 
      echo '<?xml version="1.0"?>'; 
      echo "<result>"; 
      $xml_array = json_decode(json_encode($result), true); 
      $this->xmlWalker($xml_array, "start"); 
      echo "</result>"; 
     } else { //catch all why not 
      header('Content-type: application/json'); 
      echo json_encode($result); 
     } 
    } 

    protected function xmlWalker($xml_array, $parent) { 
     foreach($xml_array as $tag => $value) { 
      if ((int)$tag === $tag) { 
       $tag = mb_substr($parent, 0, -1); 
      } 
      echo "<" .$tag. ">"; 
      if (is_array($value)) { 
       $this->xmlWalker($value, $tag); 
      } else { 
       echo $value; 
      } 
      echo "</" .$tag. ">"; 
     } 
    } 

나는 이것이 내가 고스트 솔루션을 사용하여 숫자 태그에 대한 문제가 있었다

1

도움이되기를 바랍니다. 나는에이 (직전 없음 추가) 숫자 태그를 제거 할을 약간 변경했다 :

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

어쨌든 그것은 속성을 사용하지 않는, 그리고 같은 이름을 가진 노드로 숫자 배열을하지 않습니다. 좀 더 자연 XML 출력 특성을 사용하여 같은 이름의 노드를 갖는 받기로

$xml = array2xml($jSON, "", false); 

에 전화를 변경

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

: 나는에 조금 더를 변경했습니다.

-1

내장 함수가이를 수행 할 수 있습니다.

$xml = new SimpleXMLElement('<root/>'); 
$arr = json_decode($your_array, true); 
array_walk_recursive($arr, array ($xml,'addChild')); 
관련 문제