可以使用PHP中的json_encode()
函数将JSON数组转换为PHP数组,然后使用SimpleXMLElement类将PHP数组转换为XML格式。以下是一个示例代码:
$json = '{ "person": { "name": "John Doe", "age": 30, "email": "johndoe@example.com" } }'; $array = json_decode($json, true); $xml = new SimpleXMLElement(''); array_walk_recursive($array, array($xml, 'addChild')); print $xml->asXML();
上面的代码将$json
中的JSON数组转换为PHP数组,然后通过array_walk_recursive()
函数将数组转换为XML格式,并使用SimpleXMLElement()
类创建XML对象。最后,通过调用asXML()
方法获取XML字符串并打印出来。