Using the PHP Function LibXMLGetErrors
When using an XML file, it's important to know whether the XML is valid. Otherwise, you may run into issues when trying to import that XML into your WordPress site. You can validate your XML feed by opening it in a web browser to see if any errors are encountered.
Luckily, there is a function within PHP that will display any errors encountered when parsing an XML document. This function is called libxml_get_errors and will return an array of LibXMLError objects when given an XML string or document.
To use this function, simply pass the XML to libxml_get_errors() and it will return an array of error objects if any are found or an empty array if no errors are detected. This function was introduced in PHP Version 5 and works with all later versions of PHP.
Using this function will help you troubleshoot any XML issues that may be encountered while running your scripts. It's worth mentioning that using this function could cause your script to generate notices when an XML error occurs, however you can avoid these notices by calling isset() after every if(not($var)) statement in the XML file. This will not only avoid any possible notices but also make your code more clearer.