How can you output data extracted from an XML file using SimpleXML in a structured format, such as a table?
To output data extracted from an XML file using SimpleXML in a structured format, such as a table, you can loop through the XML elements and display them within HTML table tags. This can be achieved by accessing the XML elements using SimpleXML functions and then echoing the data within table rows and cells.
<?php
$xml = simplexml_load_file('data.xml');
echo '<table>';
echo '<tr><th>Name</th><th>Age</th></tr>';
foreach ($xml->person as $person) {
echo '<tr>';
echo '<td>' . $person->name . '</td>';
echo '<td>' . $person->age . '</td>';
echo '</tr>';
}
echo '</table>';
?>
Keywords
Related Questions
- How can debugging techniques be utilized to identify and fix errors in PHP code?
- What is the best approach to handle generating a unique key in PHP and checking if it already exists in a database?
- In what ways can PHP developers optimize their code for better performance and readability when implementing pagination functionality, as demonstrated in the forum thread?