PHP convert XML to JSON

前端 未结 20 1457
生来不讨喜
生来不讨喜 2020-11-22 06:25

I am trying to convert xml to json in php. If I do a simple convert using simple xml and json_encode none of the attributes in the xml show.

$xml = simplexml         


        
相关标签:
20条回答
  • 2020-11-22 06:56

    I've used Miles Johnson's TypeConverter for this purpose. It's installable using Composer.

    You could write something like this using it:

    <?php
    require 'vendor/autoload.php';
    use mjohnson\utility\TypeConverter;
    
    $xml = file_get_contents("file.xml");
    $arr = TypeConverter::xmlToArray($xml, TypeConverter::XML_GROUP);
    echo json_encode($arr);
    
    0 讨论(0)
  • 2020-11-22 06:56
    This is better solution
    
    $fileContents= file_get_contents("https://www.feedforall.com/sample.xml");
    $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
    $fileContents = trim(str_replace('"', "'", $fileContents));
    $simpleXml = simplexml_load_string($fileContents);
    $json = json_encode($simpleXml);
    $array = json_decode($json,TRUE);
    return $array;
    
    0 讨论(0)
  • 2020-11-22 06:59

    A common pitfall is to forget that json_encode() does not respect elements with a textvalue and attribute(s). It will choose one of those, meaning dataloss. The function below solves that problem. If one decides to go for the json_encode/decode way, the following function is advised.

    function json_prepare_xml($domNode) {
      foreach($domNode->childNodes as $node) {
        if($node->hasChildNodes()) {
          json_prepare_xml($node);
        } else {
          if($domNode->hasAttributes() && strlen($domNode->nodeValue)){
             $domNode->setAttribute("nodeValue", $node->textContent);
             $node->nodeValue = "";
          }
        }
      }
    }
    
    $dom = new DOMDocument();
    $dom->loadXML( file_get_contents($xmlfile) );
    json_prepare_xml($dom);
    $sxml = simplexml_load_string( $dom->saveXML() );
    $json = json_decode( json_encode( $sxml ) );
    

    by doing so, <foo bar="3">Lorem</foo> will not end up as {"foo":"Lorem"} in your JSON.

    0 讨论(0)
  • 2020-11-22 07:04

    I guess I'm a bit late to the party but I have written a small function to accomplish this task. It also takes care of attributes, text content and even if multiple nodes with the same node-name are siblings.

    Dislaimer: I'm not a PHP native, so please bear with simple mistakes.

    function xml2js($xmlnode) {
        $root = (func_num_args() > 1 ? false : true);
        $jsnode = array();
    
        if (!$root) {
            if (count($xmlnode->attributes()) > 0){
                $jsnode["$"] = array();
                foreach($xmlnode->attributes() as $key => $value)
                    $jsnode["$"][$key] = (string)$value;
            }
    
            $textcontent = trim((string)$xmlnode);
            if (count($textcontent) > 0)
                $jsnode["_"] = $textcontent;
    
            foreach ($xmlnode->children() as $childxmlnode) {
                $childname = $childxmlnode->getName();
                if (!array_key_exists($childname, $jsnode))
                    $jsnode[$childname] = array();
                array_push($jsnode[$childname], xml2js($childxmlnode, true));
            }
            return $jsnode;
        } else {
            $nodename = $xmlnode->getName();
            $jsnode[$nodename] = array();
            array_push($jsnode[$nodename], xml2js($xmlnode, true));
            return json_encode($jsnode);
        }
    }   
    

    Usage example:

    $xml = simplexml_load_file("myfile.xml");
    echo xml2js($xml);
    

    Example Input (myfile.xml):

    <family name="Johnson">
        <child name="John" age="5">
            <toy status="old">Trooper</toy>
            <toy status="old">Ultrablock</toy>
            <toy status="new">Bike</toy>
        </child>
    </family>
    

    Example output:

    {"family":[{"$":{"name":"Johnson"},"child":[{"$":{"name":"John","age":"5"},"toy":[{"$":{"status":"old"},"_":"Trooper"},{"$":{"status":"old"},"_":"Ultrablock"},{"$":{"status":"new"},"_":"Bike"}]}]}]}
    

    Pretty printed:

    {
        "family" : [{
                "$" : {
                    "name" : "Johnson"
                },
                "child" : [{
                        "$" : {
                            "name" : "John",
                            "age" : "5"
                        },
                        "toy" : [{
                                "$" : {
                                    "status" : "old"
                                },
                                "_" : "Trooper"
                            }, {
                                "$" : {
                                    "status" : "old"
                                },
                                "_" : "Ultrablock"
                            }, {
                                "$" : {
                                    "status" : "new"
                                },
                                "_" : "Bike"
                            }
                        ]
                    }
                ]
            }
        ]
    }
    

    Quirks to keep in mind: Several tags with the same tagname can be siblings. Other solutions will most likely drop all but the last sibling. To avoid this each and every single node, even if it only has one child, is an array which hold an object for each instance of the tagname. (See multiple "" elements in example)

    Even the root element, of which only one should exist in a valid XML document is stored as array with an object of the instance, just to have a consistent data structure.

    To be able to distinguish between XML node content and XML attributes each objects attributes are stored in the "$" and the content in the "_" child.

    Edit: I forgot to show the output for your example input data

    {
        "states" : [{
                "state" : [{
                        "$" : {
                            "id" : "AL"
                        },
                        "name" : [{
                                "_" : "Alabama"
                            }
                        ]
                    }, {
                        "$" : {
                            "id" : "AK"
                        },
                        "name" : [{
                                "_" : "Alaska"
                            }
                        ]
                    }
                ]
            }
        ]
    }
    
    0 讨论(0)
  • 2020-11-22 07:05

    Found FTav's answer the most useful as it is very customizable, but his xml2js function has some flaws. For instance, if children elements has equal tagnames they all will be stored in a single object, this means that the order of elements will not be preserved. In some cases we really want to preserve order, so we better store every element's data in a separate object:

    function xml2js($xmlnode) {
        $jsnode = array();
        $nodename = $xmlnode->getName();
        $current_object = array();
    
        if (count($xmlnode->attributes()) > 0) {
            foreach($xmlnode->attributes() as $key => $value) {
                $current_object[$key] = (string)$value;
            }
        }
    
        $textcontent = trim((string)$xmlnode);
        if (strlen($textcontent) > 0) {
            $current_object["content"] = $textcontent;
        }
    
        if (count($xmlnode->children()) > 0) {
            $current_object['children'] = array();
            foreach ($xmlnode->children() as $childxmlnode) {
                $childname = $childxmlnode->getName();
                array_push($current_object['children'], xml2js($childxmlnode, true));
            }
        }
    
        $jsnode[ $nodename ] = $current_object;
        return $jsnode;
    }
    

    Here is how it works. Initial xml structure:

    <some-tag some-attribute="value of some attribute">
      <another-tag>With text</another-tag>
      <surprise></surprise>
      <another-tag>The last one</another-tag>
    </some-tag>
    

    Result JSON:

    {
        "some-tag": {
            "some-attribute": "value of some attribute",
            "children": [
                {
                    "another-tag": {
                        "content": "With text"
                    }
                },
                {
                    "surprise": []
                },
                {
                    "another-tag": {
                        "content": "The last one"
                    }
                }
            ]
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:06

    Optimizing Antonio Max answer:

    $xmlfile = 'yourfile.xml';
    $xmlparser = xml_parser_create();
    
    // open a file and read data
    $fp = fopen($xmlfile, 'r');
    //9999999 is the length which fread stops to read.
    $xmldata = fread($fp, 9999999);
    
    // converting to XML
    $xml = simplexml_load_string($xmldata, "SimpleXMLElement", LIBXML_NOCDATA);
    
    // converting to JSON
    $json = json_encode($xml);
    $array = json_decode($json,TRUE);
    
    0 讨论(0)
提交回复
热议问题