How do I Convert jSON to XML

后端 未结 4 859
离开以前
离开以前 2020-12-31 22:05

Before I begin, I know there are a bunch of questions similar to this, but trust me, I read all, if not most of them. I tried a bunch of solutions, but none of them seem to

相关标签:
4条回答
  • 2020-12-31 22:37

    I had problems with numeric tags using Ghost solution. I had to changed it a bit to remove the numeric tags (just adding a n before):

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

    Anyways it does not use attributes, and it does not make arrays with numbers as nodes with same name. I have changed it a bit more to:

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

    Changing the call to

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

    With this you get a more natural xml output using attributes and having nodes with same name.

    0 讨论(0)
  • 2020-12-31 22:44

    The built in functions can do this.

    $xml = new SimpleXMLElement('<root/>');
    $arr = json_decode($your_array, true);
    array_walk_recursive($arr, array ($xml,'addChild'));
    
    0 讨论(0)
  • 2020-12-31 22:55

    Instead of feeding your function an object, try to feed an array instead:

    $jSON = json_decode($raw_data, true);
                                //  ^ add second parameter flag `true`
    

    Example:

    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

    0 讨论(0)
  • 2020-12-31 22:57

    This really isn't too hard to do, I had a similar problem and just solved it by doing the following, thought I would share it just in case anyone else was trying to do it. It is done very simply.

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

    I hope this helps

    0 讨论(0)
提交回复
热议问题