How to convert a stdClass object to a SimpleXMLElement object I have a JSON file from a web service and want to use this data with a Drupal module. However, this module expects XML data. So I have the idea of overwriting the module. To do this, I need to convert the JSON file in XML, and ensure the file changes from JSON to PHP array.

Does any method or library exist to do this or convert .json file to .xml directly?

Now I have an array that is like the following one:

Array(
  [0] =&gt; stdClass Object(
    [sponsor] =&gt; stdClass Object(
      [created_at] =&gt;  
    )
  )
)


I need to convert this array in: 

SimpleXMLElement Object(
  [@attributes] =&gt; Array(
    [type] =&gt; Array(
      [sponsor] =&gt; Array(
        [0] =&gt; SimpleXMLElement Object(
          [created-at] =&gt;
        )
      )
    )
  )
)
Using the XML Serializer package, you can use the following code to make the conversion, as reported in this answer on Stack Overflow.

function json_to_xml($json) {
  $serializer = new XML_Serializer();
  $obj = json_decode($json);

  if ($serializer-&gt;serialize($obj)) {
    return $serializer-&gt;getSerializedData();
  }
  else {
    return NULL;
  }
}


Alternatively, you can use the following code give in this answer for the same question.

require_once 'XML/Serializer.php';

$data = json_decode($json, true)

// An array of serializer options.
$serializer_options = array (
  'addDecl' =&gt; TRUE,
  'encoding' =&gt; 'ISO-8859-1',
  'indent' =&gt; '  ',
  'rootName' =&gt; 'json',
  'mode' =&gt; 'simplexml'
); 

$Serializer = &amp;new XML_Serializer($serializer_options);
$status = $Serializer-&gt;serialize($data);

if (PEAR::isError($status)) {
  die($status-&gt;getMessage());
}

echo '&lt;pre&gt;';
echo htmlspecialchars($Serializer-&gt;getSerializedData());
echo '&lt;/pre&gt;';