PHP convert XML to JSON

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

    Try to use this

    $xml = ... // Xml file data
    
    // first approach
    $Json = json_encode(simplexml_load_string($xml));
    
    ---------------- OR -----------------------
    
    // second approach
    $Json = json_encode(simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA));
    
    echo $Json;
    

    Or

    You can use this library : https://github.com/rentpost/xml2array

提交回复
热议问题