How can JSON be used to format and access structured XML data in PHP?
To format and access structured XML data in PHP using JSON, you can first convert the XML data into a JSON format using PHP functions like json_encode(). This allows you to easily access and manipulate the structured data in a more convenient way. Once the XML data is converted to JSON, you can then use PHP's json_decode() function to decode the JSON data back into an array or object for easy access.
// Sample XML data
$xmlData = '<root><name>John Doe</name><age>30</age></root>';
// Convert XML to JSON
$xmlObject = simplexml_load_string($xmlData);
$jsonData = json_encode($xmlObject);
// Decode JSON data
$decodedData = json_decode($jsonData, true);
// Access structured data
echo $decodedData['name']; // Output: John Doe
echo $decodedData['age']; // Output: 30