How to Read XML with PHP 


               


How to Read XML with PHP



PHP



In this article, we show how to read XML, either an XML file or a string containing XML, with PHP.

This means that we use PHP code to read the contents of an XML document or a string containing XML.

We'll show how to do these things below.

Reading an XML File

First, we'll go over how to read an XML file. This is a file with a .xml extension that contains full XML code. A regular, typical XML file.

There's many ways that this can be done, but probably the easiest way to read an XML file with PHP is to use the simplexml_load_file() function. Inside the parameters of this function, you specify the XML document or file that you want to read.

This function loads the entire document into memory and then you can display the document using the PHP print_r() function which is able to show the contents of the array.

As an example XML file, I have an XML named books.xml. This is an XML file that contains book elements which show the title and the publication date of each book.

We will be reading this file as our example code shown below.

PHP Code

The PHP code to read the contents of the XML file, books.xml, is shown below.



So, in this code, we create a variable named $file and set it equal to the simplexml_load_string('books.xml);

This loads in the books.xml function into memory.

We then print out the contents of the $file variable which holds the XML document using the PHP print_r() function. The print_r() function is a function which can show us the contents of a PHP array.

So running the above PHP yields the following output below.

Actual PHP Output


SimpleXMLElement Object
(
    [book] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [Booktitle] => The Wandering Oz
                    [PublicationDate] => 2007
                )

            [1] => SimpleXMLElement Object
                (
                    [Booktitle] => The Roaming Fox
                    [PublicationDate] => 2009
                )

            [2] => SimpleXMLElement Object
                (
                    [Booktitle] => The Dominant Lion
                    [PublicationDate] => 2012
                )

        )

)


So the above PHP output is the output that we get when we read the XML file, books.xml, with the simplexml_load_file() function.

Looking at this output, you can see that PHP converts the XML document into an array. So the simplexml_load_file() takes the elements from the XML file and puts them in an array.

The <pre></pre> tags makes the output very readable.

The first element of the root element books is book that has a title of "The Wandering Oz" and a publication date of 2007. So you can see [0] next to this set of data.

The second element has a title of "The Roaming Fox" and a publication date of 2009. So you can see [1] next to this set of data.

The third element has a title of "The Dominant Lion" and a publication date of 2012. So you can see [2] next to this set of data.

So this is how with the simplexml_load_file() function, we can read an XML file with PHP.

Reading a String Containing XML

Now that we've shown how to read an XML file, we'll now show how to read a string containing XML.

PHP also has a built-in function, simplexml_load_string(), that can read a string containing XML.

It works in the same way as the simplexml_load_file() function except that it reads a string instead of a file.

This is demonstrated in the code below.



So with this PHP code above, we read the XML string contained in the variable $classics.

We read this string using the simplexml_load_string() function and place the variable holding the string, $classics, into this function as the parameter.

We then output the result using the PHP print_r() function, just as before. And as before, we use the pre tags for better formatting and readability.

Below is the results of this PHP output.

Actual PHP Output


SimpleXMLElement Object
(
    [movie] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [movietitle] => Jurassic Park
                    [year] => 1993
                )

            [1] => SimpleXMLElement Object
                (
                    [movietitle] => Home Alone 2
                    [year] => 1992
                )

        )

)



Related Resources

How to Create an XML Document with PHP

How to Parse an XML Document Using PHP

HTML Comment Box is loading comments...