PHP convert XML to JSON

前端 未结 20 1492
生来不讨喜
生来不讨喜 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 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);
    

提交回复
热议问题