As I said in the previous section, the “simpleXML” extension comes with some additional methods for parsing XML data, which can be quite useful, depending on the requirements of your PHP 5 applications. The first method that I’ll explain is called “children()”, and as its name suggests, is aimed at finding all the child nodes of a parent element. To clarify how it can be used, I’ll use the following XML data string: $xmlstr=<<<XML As you can see, I created a new string of XML data, so you can have an accurate idea of how to access the child nodes of all the <gender> nodes. Now, take a look at the following script, which uses the “children()” method for performing this task: // example finding child nodes of <gender> nodes with the On this occasion, the above script utilizes the “children()” method inside two “foreach” loops, in order to find all the child nodes that belong to the corresponding <gender> elements, which outputs the following result: Name node found with the following value: John Doe Well, if the previous example is a little bit harder to understand, check out this one, which uses the following XML string and certainly is much simpler to read and code: $xmlstr=<<<XML require_once 'xml_string.php'; // displays the following output Name node with the following value John Doe Actually, the example shown above is rather trivial, but I just want you to grasp correctly the logic behind the “children()” method. Did you get it? I hope so. Finally, the two methods that I’d like to show you are called “attributes()” and “simple_import_dom()” respectively. Obviously, the first one is focused on retrieving all the attributes that correspond to a specific XML node, while the second one is useful for importing a new DOM document as XML data. With reference to the “attributes()” method, here is an example that teaches you how it can be implemented: $xmlstr=<<<XML // example using the 'attributes()' method // displays the following output Attribute name found with the following value: John Doe As you can see, the “attributes()” method allows you to obtain all the attributes of a particular element within a XML string, which are returned as an associative array. In this case, I used basic XML data, so that you can easily understand how this method works. After demonstrating how the “attributes()” method does its thing, take a little break and examine the code snippet below, which shows how to implement the “simplexml_import_dom()” method: // example using the 'import_dom()' method // displays the following output Name of first user: John Doe I purposely kept the above example rather simple, since the XML DOM is a huge topic, and certainly is out of the scope of this series. In short, the “simplexml_import_dom()” method can be used as an alternative way to load XML data strings onto an object. It lets you obtain results similar to using both the “simplexml_load_file()” and “simplexml_load_string()” methods. However, if you want to take the shortest path to parsing XML data, I suggest you to use these methods instead. Wrapping up Finally, we’re done. Over the course of this series, I introduced some of the most important functions that are included within the “simpleXML” PHP 5 extension. As you saw, this library eventually might fit the requirements of many applications that don’t demand complex parsing of XML data. If your next PHP-driven project falls under this category, go ahead and use it with no restrictions. See you in the next PHP tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|