Home arrow PHP arrow Page 3 - Working with Multiple Document Nodes with the DOM XML Extension in PHP 5

Loading XML data from a specified text file with the load() method - PHP

Welcome to the fourth article of the series "A quick overview of the DOM XML extension in PHP 5." By means of easy-to-grasp hands-on examples, this series equips you with a friendly guide to using the most relevant methods that come bundled with the DOM XML PHP extension to help you work with XML documents in a truly painless way.

TABLE OF CONTENTS:
  1. Working with Multiple Document Nodes with the DOM XML Extension in PHP 5
  2. Getting a collection of XML nodes
  3. Loading XML data from a specified text file with the load() method
  4. Reading data from a specified XML string with the loadXML() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 18, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: