Convert XML to json and then a PHP array

(you go via json even if you just want the PHP array)

    //Make sure simplexml is installed (it should be there by default with php, but it seems to be omitted in some installs.
    if (!function_exists('simplexml_load_file'))
      die('Server config issue - simpleXML functions are not available.');    //You can install this on ubuntu using: sudo apt-get install php-xml
    
    $RequestXml = "<RootElement><FieldName1>1</FieldName1><FieldName2>123</FieldName2></RootElement>";
    
    $xml = simplexml_load_string($RequestXml, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    $RequestArray = json_decode($json, TRUE);
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *