What are the best practices for formatting and displaying XML data in PHP to avoid unwanted characters or formatting issues?

When displaying XML data in PHP, it's important to properly encode the data to avoid unwanted characters or formatting issues. One way to achieve this is by using the htmlspecialchars() function to encode special characters in the XML data before displaying it.

// Sample XML data
$xmlData = "<data><name>John Doe</name><age>30</age></data>";

// Encode XML data
$encodedXmlData = htmlspecialchars($xmlData);

// Display encoded XML data
echo "<pre>" . $encodedXmlData . "</pre>";