As I noted in the previous section, the DOM XML extension offers a specific method whose function is based on reading XML data straight from a given string. The method is called “loadXML()”, and an example of how to use is listed below, so take a look at it, please: // example on loading XML data from an existing string using the 'loadXML()' method $xml='<?xml version="1.0" encoding="iso-8859-1"?><headlines><headline id="economics"><image>image1.jpg</image><url>Link for headline 1 goes here</url><text>Text for headline 1 goes here</text></headline><headline id="sports"><image>image2.jpg</image><url>Link for headline 2 goes here</url><text>Text for headline 2 goes here</text></headline></headlines>'; $dom=new DOMDocument(); // load XML data from string $dom->loadXML($xml); // tell the browser the output is XML via the 'Content-Type' HTTP header header('Content-Type: text/xml'); // display DOM document echo $dom->saveXML(); /* displays the following <?xml version="1.0" encoding="iso-8859-1"?> <headlines> <headline id="economics"> <image>image1.jpg</image> <url>Link for headline 1 goes here</url> <text>Text for headline 1 goes here</text> </headline> <headline id="sports"> <image>image2.jpg</image> <url>Link for headline 2 goes here</url> <text>Text for headline 2 goes here</text> </headline> </headlines> */ Definitely, reading XML data from a specified string by using the previous “loadXML()” method is not only simple, but pretty fun, don’t you think? At the very least, you’ll have to agree with me that the DOM XML extension makes reading XML data either from a specific file or from a plain string a no-brainer process. Here’s my final suggestion: feel free to tweak all of the code samples included here to acquire a better understanding of working with the DOM XML PHP extension. Have fun! Final thoughts As I said before, we’ve come to a close. However, hopefully this fourth chapter of the series has opened your eyes a bit more about the use of the methods reviewed earlier. As I showed you, they can be used to handle collections of XML nodes, and even complete XML files and strings. In the next part of the series, I’m going to show you how to utilize the DOM XML library to read plain HTML files, as well as build HTML documents by using their respective DOM-based representation. Now that you’re aware of the topics that will be discussed in the upcoming article, you won’t want to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|