Join David Powers for an in-depth discussion in this video Loading an XML document, part of Up and Running with PHP SimpleXML.
- View Offline
- There are several ways to load data into SimpleXML. We'll look at the most common starting with loading this file, courses.xml, which you can find in the xml folder of the Exercise Files. The root element is called courses and it has two top level child elements called course. Each course element has a single attribute called url, and nested inside each course are child elements for title, author, description and so on. One of the child elements, subjects, has children of its own and they're called subject.
So let's load this into SimpleXML. I'm gonna to switch to load.php. I've already created a variable that stores the relative path to courses.xml. We're in load.php so we need to go up two levels to the Chapter 1 folder to be able to get to the xml folder and courses.xml. I've also used echo to add a couple of pre tags to make it easier to view the output. Although SimpleXML creates an object, the most common way to load an XML document is with a function.
If the XML is in an external file, you use SimpleXML load_file. So we'll create a variable to store our object. We'll call that courses and SimpleXML load_file. We pass it the path to the file. In this case we're using a local file, but if the XML document is on a remote server, SimpleXML load_file also accepts a URL instead of a file path. We can use print_r to inspect the contents of a SimpleXML element object, so between the pre tags, print_r and we'll pass it our object, courses.
Save and then load it into a browser. The output looks like a multidimensional array and print_r even describes some parts as arrays. For example, the top level course element, that's described as an array. Attributes is described as an array, and so is subject down here. This is rather misleading because they're not arrays, they're iterable objects. What that means in practice is that you can't use array functions with them, but you can treat them like an array by using a foreach loop to loop over them.
So we can use foreach to loop over the course elements. We can use array notation to get the url attribute of each course element, and we can also loop over the subject elements. These other elements, such as title, author, description and so on, they become properties of each course element. Notice that we're dealing not only with one single SimpleXML element object at the top, the inner elements are also objects, and this is very useful because it means the inner elements also have access to SimpleXML methods.
So that's how you load XML data into SimpleXML from an external file or a URL. But if the XML data is stored as a string, you need to use a different function, so let's get back to the editing program. In string.txt there's some XML data, which is stored as a heredoc, so if we select all that, copy it, go back to load.php and paste it in, we can then load this into SimpleXML using SimpleXML load_string.
So we'll save this as cars and then SimpleXML load_string and we pass it our string as the argument. We can inspect it using print_r. Let's just comment out that one. Print_r cars. If we save that and refresh the browser, here's the content of the XML data that was loaded from a string. We can see that we've got the top level element here, the top level child, which is car and there are two cars inside there.
So that's using SimpleXML load_string. You can also use the SimpleXML element constructor to load an XML document, so let's get back to the code. By default the constructor expects the XML to be a string. So we'll use this string again. We'll save that as cars2. We have the new operator and we have the new keyword, and then a SimpleXML element, and we pass it our string as a sole argument.
So we'll use print_r to inspect that. If we save and refresh the browser, we get exactly the same output as before. If the XML is in an external file or needs to be retrieved from a URL, you need to pass two more arguments to this SimpleXML element constructor. The second argument sets options for LIBXML and you do that using the constants that can be found in this page in the PHP online documentation.
LIBXML is the underlying library that PHP uses to pass the XML. In most cases you don't need to set any options, but you do need to have that second argument, and the way to get round that is you use null as the second argument. Let's get back to the code. We'll use our file, our courses.xml, so we'll call that courses2 and then the new keyword SimpleXML element.
The first argument will our file. The second argument are the LIBXML options. We don't want any so we just use the null keyword. The third argument is the Boolean true and this tells the SimpleXML element constructor that we're using an external file or a URL. We'll just use print_r to inspect that again. If we save that and refresh the browser, we get the structure of the XML data that's in courses.xml.
So that's how you load from a file with the SimpleXML element constructor. You need to have three arguments, the file, LIBXML options, and then the Boolean true. Most of the time XML is in an external file so it's much simpler to use the SimpleXML load_file function rather than to use the SimpleXML element constructor with three arguments. But if the XML has been stored as a string, you need to use the SimpleXML load_string function or the SimpleXML element constructor with a single argument that needs to be XML data as a string.
For most of the course I'll be using SimpleXML load_file. This expects a file path or URL as its first argument. All the functions and the SimpleXML element constructor accept optional arguments but you rarely need to use them except when using SimpleXML with an iterator from the standard PHP library. We'll come back to that in Chapter 4. In this video we also saw how to inspect the contents of a SimpleXML element object with print_r.
It has the same structure as a multidimensional array, but each element is an object, making it possible to burrow down within the XML structure but still have access to SimpleXML methods.
- Loading an XML document
- Converting values to strings
- Handling errors
- Working with XML namespaces
- Using XPath
- Consuming an RSS feed with XML
- Adding and editing XML elements and attributes