Handling XML errors

- Up to now, we've assumed…that the XML document being loaded…with SimpleXML is well-formed.…But the rules of XML are very strict.…Even a minor error will prevent the document…from being parsed correctly.…The code in this file loads courses4.xml…and then attempts to display it.…But if we load this into a browser,…we get this long list of warnings with error messages.…The messages are not only difficult to understand,…the really noticeable thing is…they're all reported as being on line 4.…

And line 4 is where we used simplexml_load_file…to load the XML document.…Rather than attempting to struggle…through the list of errors…to identify the problems,…it's easier to use PHP's special…error handling for XML.…Before loading the XML document,…you need to suppress errors generated by LibXML.…That's the XML parser used by PHP.…And you do that by calling the…libxml_use_internal_errors function.…

So let's add a new line on line 3…and then we'll call the…libxml_use_internal_errrors function.…And we need to parse…that the Boolean true as an argument.…

Resume Transcript Auto-Scroll

Author

Released

4/15/2015

Most PHP programmers will eventually run into an XML data set. The easiest way to deal with this data is using the SimpleXML extension for PHP. In this course, David Powers walks through loading and extracting data from a basic XML document, working with namespaces, and editing XML elements and attributes with SimpleXML and the XPath query language. He also shows practical uses for XML, such as displaying text as well as images from an RSS feed, and the process for generating new XML documents from scratch with SimpleXML.