PHP
  Home arrow PHP arrow Page 3 - Working with Multiple Document Nodes with the DOM XML Extension in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek