PHP
  Home arrow PHP arrow Page 3 - Working with Multiple Document Nodes w...
CIO Insight
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Working with Multiple Document Nodes with the DOM XML Extension in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-18

    Table of Contents:
  • Working with Multiple Document Nodes with the DOM XML Extension in PHP 5
  • Getting a collection of XML nodes
  • Loading XML data from a specified text file with the load() method
  • Reading data from a specified XML string with the loadXML() method

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Minimize the cost of deploying database applications. Advantage Database Server or Microsoft SQL Server – Which One is Right for You? Learn now!

    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
    (Page 3 of 4 )

    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


       · In this fourth tutorial of the series, you’ll learn how to get access to multiple...
       · I'm finding this collection of articles extremely long winded. Every article...
       · Thank you for commenting on my PHP article. Sorry to hear you didn’t like it,...
     

       

    PHP ARTICLES

    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5
    - Working with Multiple Document Nodes with th...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway