PHP convert XML to JSON

前端 未结 20 1948
生来不讨喜
生来不讨喜 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:12

    If you are ubuntu user install xml reader (i have php 5.6. if you have other please find package and install)

    sudo apt-get install php5.6-xml
    service apache2 restart
    
    $fileContents = file_get_contents('myDirPath/filename.xml');
    $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
    $fileContents = trim(str_replace('"', "'", $fileContents));
    $oldXml = $fileContents;
    $simpleXml = simplexml_load_string($fileContents);
    $json = json_encode($simpleXml);
    

提交回复
热议问题