Whenever our work requires PHP reading XML document data from string, we use a function called simplexml_load_string().
Imagine we have a variable $my_data
that contains XML data. The following code example shows how it would look first declared:
Now, in the example below, simplexml_load_string()
is used to PHP read XML data straight from previously mentioned variable :
Here is the output we get in XML:
Error Handling Tip: Use the libxml functionality to retrieve all XML errors when loading the document and then iterate over the errors. The following example tries to load a broken XML string:
Now, this is the output we would get in XML in such case:
The PHP simplexml_load_file()
function is used to read XML data from a file.
Assume we have an XML file called "Hello.html", that looks like this:
The example below shows how to use the simplexml_load_file()
function to read XML data from a file:
Output :
SimpleXML: Summary