PHP
  Home arrow PHP arrow Page 4 - 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? 
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 - Reading data from a specified XML string with the loadXML() method
    ( Page 4 of 4 )

    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!



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT