- Before diving into the code, let's take a look at What makes SimpleXML so simple? The code consists of two built in classes. SimpleXMLElement is the main class that you work with. It gives you access to the content of the XML document and has methods to add new elements and attributes. SimpleXMLIterator normally works in the background allowing you to use SimpleXMLElement in a loop. There are also three functions. You use these to load an XML document from a string or file, or to import an XML mode from the DOM extension.
What makes SimpleXML worthy of its name is its basic use. You load the XML document and use a foreach loop to iterate over the content. Elements of property of the SimpleXMLElement object those are accessed with the arrow operator. Attributes are accessed through array notation and you can use echo or print to display the values. The process is very intuitive. This is an XML document with a route element called courses.
Once loaded, the route element becomes a SimpleXMLElement object containing the data in a tree-like structure. It doesn't matter what name you assign to the object, but using the same name as the route element makes it easier to understand how to access other elements. For this example, let's call the object courses. Child elements become properties of the object. So to access course, just add the arrow operator and the name of the element.
This example shows only one course element. But let's say there are multiple course elements. To access each one, use a foreach loop and assign it to an alias. To keep the code easy to follow, it's a good idea to use the element name as the alias. So inside the loop the top level children can be accessed by the variable course. Attributes are treated like an associative array. So inside the loop course ID gets the ID attributes of each course element.
Title is one level deeper in the XML structure. So you access its value with the arrow operator and the element name. You access subjects in the same way. But subjects has child elements of its own. To access them, just chain the name of the child elements to course subjects and use another foreach loop. SimpleXML works best when you know the structure of the XML document. Study the names of the elements and attributes you want to access, and note how far down the hierarchy they're nested inside the root element.
The root element itself becomes the SimpleXMLElement object that contains all the data. Use foreach to loop over the child elements. Access child elements with the arrow operator. When elements are nested several levels deep, chain the names of the ancestor elements to get to the one you want. Access attributes using the attribute name in quotes between square brackets, just like an associate of array. Using SimpleXML to extract data from XML is very intuitive.
In a word, it's simple. SimpleXML.
- 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