PHP implements a DOM Level 2 compliant parser, allowing you to work with HTML using familiar methods like getElementById() or appendChild().
Note that PHP will emit warnings about any problems with the HTML, especially if you are importing a document fragment. To avoid these warnings, tell the DOM library (libxml) to handle its own errors by calling libxml_use_internal_errors() before importing your HTML. You can then use libxml_get_errors() to handle errors if needed.
SimpleXML is a PHP library which provides an easy way to work with XML documents (especially reading and iterating through XML data).
The only restraint is that the XML document must be well-formed.
Parsing XML using procedural approach
Parsing XML using OOP approach
Accessing Children and Attributes
When SimpleXML parses an XML document, it converts all its XML elements, or nodes, to properties of the resulting SimpleXMLElement object
In addition, it converts XML attributes to an associative array that may be accessed from the property to which they belong.
When you know their names:
The major drawback of this approach is that it is necessary to know the names of every element and attribute in the XML document.
When you don't know their names (or you don't want to know them):
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0