As I stated in the previous section, the DOM XML extension comes equipped with some helpful methods for moving entire XML strings from a text file to a variable and vice versa. Based on this concept, I’m going to show you how to use the “load()” method, whose function is basically loading the contents of a specified XML file onto the web server’s memory. Having explained briefly how this method works, suppose that there’s an XML file that contains data about some hypothetical headlines, whose structure looks like this: <?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> <headline id="jetset"> <image>image3.jpg</image> <url>Link for headline 3 goes here</url> <text>Text for headline 3 goes here</text> </headline> <headline id="technology"> <image>image4.jpg</image> <url>Link for headline 4 goes here</url> <text>Text for headline 4 goes here</text> </headline> <headline id="art"> <image>image5.jpg</image> <url>Link for headline 5 goes here</url> <text>Text for headline 5 goes here</text> </headline> </headlines> Indeed, the above XML file is very understandable, since it’s been populated with a few nodes which store data about the mentioned headlines, including their respective titles, thumbnails, and links as well. In addition, each headline has an ID attribute that helps to separate them into different categories. This is pretty simple to follow, right? Now that you have seen how the previous XML file looks, please examine the code of the following hands-on example, which shows how to read the contents of this file by using the handy “load()” method: // example on loading XML data from an existing file using the 'load()' method $dom=new DOMDocument(); // load XML data from file $dom->load('headlines.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> <headline id="jetset"> <image>image3.jpg</image> <url>Link for headline 3 goes here</url> <text>Text for headline 3 goes here</text> </headline> <headline id="technology"> <image>image4.jpg</image> <url>Link for headline 4 goes here</url> <text>Text for headline 4 goes here</text> </headline> <headline id="art"> <image>image5.jpg</image> <url>Link for headline 5 goes here</url> <text>Text for headline 5 goes here</text> </headline> </headlines> */ As you can see, reading the contents of a specified XML file with the “load()” method is a straightforward process that can be performed with minor hassles. In this case, the XML data is transferred directly from the source file to the web server’s memory, and then echoed to the browser via the “saveXML()” method that you already learned in previous articles of the series. Okay, at this point, I've taught you how to read the contents of a simple XML file by means of the “load()” method included with the DOM XML extension. Yet this library features plenty of methods for loading XML data from remote sources. For example, there is “loadXML()”, which has the ability to read XML contents from a specific string. Therefore, assuming that you’re interested in learning how to use this method, in the last section of this article I’m going to provide you with a practical example that will show it in action. Of course, this particular topic will be covered in detail in the next few paragraphs, so please click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|