How can PHP developers efficiently parse XML data and convert it into an array of objects for further manipulation and display in HTML?
To efficiently parse XML data and convert it into an array of objects in PHP, developers can use the SimpleXML extension. This extension provides an easy way to read, write, and manipulate XML data. By using SimpleXML functions, developers can parse XML data, convert it into objects, and then manipulate these objects as needed before displaying them in HTML.
$xmlData = '<data>
<item>
<name>Item 1</name>
<price>10.99</price>
</item>
<item>
<name>Item 2</name>
<price>20.50</price>
</item>
</data>';
// Parse XML data
$xml = simplexml_load_string($xmlData);
// Convert XML data into an array of objects
$json = json_encode($xml);
$array = json_decode($json,TRUE);
// Display data in HTML
foreach ($array['item'] as $item) {
echo '<div>';
echo '<h3>' . $item['name'] . '</h3>';
echo '<p>$' . $item['price'] . '</p>';
echo '</div>';
}