What are the potential pitfalls of using preg_replace to format XML in PHP?

Using preg_replace to format XML in PHP can potentially lead to unintended consequences, such as altering the structure of the XML document or corrupting the data. It is recommended to use a dedicated XML parsing library, like SimpleXML or DOMDocument, to properly handle and manipulate XML data in PHP.

// Example using SimpleXML to format XML
$xmlString = '<root><item>1</item><item>2</item></root>';
$xml = simplexml_load_string($xmlString);
$prettyXml = $xml->asXML();
echo $prettyXml;