spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php / php4xml / chap10 / 3 current pageTo page 2To page 3To page 4To page 5To page 6To page 7
[next]

Professional PHP4 XML, Chapter 10: Putting It Together

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Creating Objects from XML Files

[The following is a continuation of our series of excerpts from chapter 10 of the Wrox Press title, Professional PHP4 XML. Source code for the examples discussed can be downloaded at the Wrox Web site (free e-mail registration required).]

This problem is stated as follows - we have an XML document and we want to create PHP objects from the information in the document. These objects can then be used for different tasks depending on the application that needs them. This is known as the XMLable pattern.

Example

We may have an XML file describing books, and we may want to create book objects from our XML document. This is a simplified representation of the XML document:

<?xml version="1.0"?>
<books>
  <book>
    <title>The Two Tours</title>
    <author>J.R.R Tolkien</author>
  </book>
  
  <book>
    <title>Second Foundation</title>
    <author>I.Asimov</author>
  </book>
  
  <book>
    <title>Foundation and Empire</title>
    <author>I.Asimov</author>
  </book>
</books>

And this can be the Book class:

class Book 
{
    var $title;
    var $author;

    function GetTitle() 
    {
        return $this->title;
    }
  
    function GetAuthor() 
    {
        return $this->author;
    }

    function SetTitle($title) 
    {
        $this->title = $title;
    }

    function SetAuthor($author) 
    {
        $this->author = $author;
    }
}

As usual we may think about DOM, XSLT, or SAX, but in this problem we don't have choices, let's see why.

Firstly, DOM is unacceptable in this case. We parse the XML document into the DOM tree, and then we'll be facing the same problem we had before - how to convert the DOM tree to many book objects. XSLT is too exotic for this problem; we may write an XSLT transformation that creates PHP code that creates the objects, and then eval() the transformation result.

So the obvious solution is to use SAX, where we parse the XML document and we create the objects as we collect enough information for each one of them.


home / programming / php / php4xml / chap10 / 3 current pageTo page 2To page 3To page 4To page 5To page 6To page 7
[next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: August 26, 2002
Revised: August 26, 2002

URL: http://webreference.com/programming/php/php4xml/chap10/3/