Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


The DOM Model Page 34

March 15, 2002


The XML document we are creating has an element Travelpackage with an attribute name="a". To add the attribute we use the setattr() function. setattr() has two parameters - the first string is the name of the attribute, the second string is the value of the string. When the XML document is created, we get name="value":

    $one->setattr("name", "a");

Now we can add the rest of the children of Travelpackage:

$one->new_child("Country_name", "Cuba");
$one->new_child("City", "Cayo Coco");
$one->new_child("Resort","Club Tryp Cayo Coco");
$one->new_child("Resort_rating", "4");
$one->new_child("Resort_typeofholiday","beach");
$one->new_child("Resort_watersports", "true");
$one->new_child("Resort_meals", "true");
$one->new_child("Resort_drinks","true");

One of the children of Travelpackage is Package, which is an empty node that is a parent of two other nodes. If we keep using the above syntax, then the children of Package will become children of Travelpackage. So we create a new object called $oneSub and make it a child of Travelpackage:

   $oneSub = $one->new_child("Package", "");

Now we assign the two children of Package:

$oneSubSub = $oneSub->new_child("Package_dateofdep", "5/8/89");
$oneSubSub = $oneSub->new_child("Package_price", "879");

This process is repeated for the second Travelpackage instance. We're going to build the second instance of Travelpackage using an array and loop. In this case we assign the name of the node, Country_name to the array key, and the content of the node to the array value, Cuba. The array could be populated using a database query where the key is the table column name and the value is the data in the table cell:

$two = $root->new_child("Travelpackage", "");
$two->setattr("name", "b");

$nodeName = array(
    "Country_name" => "Cuba",
    "City" => "Varadero",
    "Resort" => "Sol Club Paleras",
    "Resort_rating" => "3",
    "Resort_typeofholiday" => "beach",
    "Resort_watersports" => "false",
    "Resort_meals" => "true",
    "Resort_drinks" => "false");

while (list($key,$value) = each($nodeName)) {
    $two->new_child($key, $value);
}

$twoSub = $two->new_child("Package", "");

$twoSubSub = $twoSub->new_child("Package_dateofdep", "5/1/89");
$twoSubSub = $twoSub->new_child("Package_price", "779");

Now that we've created all the nodes for the XML document, we need to create the document. First, we create a new file, and then we dump the nodes from memory into the new file. Using the "w+" mode in fopen(), we create a new file with contents from the memory dump. This will replace any existing file of the same name:

$fp = fopen("travel.xml", "w+");

To append to an existing file we can't interact with the file like a regular file. First we have to read the file into memory, then we can add a new child and dump the memory using dumpmem() to write the newly appended file. Be sure you don't send a new root to the document:

fwrite($fp, $doc->dumpmem(), strlen($doc->dumpmem()));
fclose($fp);

The following code opens the XML file and then loads it into memory:

    $doc = xmldoc(join("", file("travel.xml")));
    $root = $doc->root(); $nodes = $root->children();

First we create an instance of the DOM object called $doc, then we assign a root and populate the nodes object with all the child nodes under the root. When the existing file has been read into the XML tree, we can add a new node or child to the root. This new node or child is called Travelpackage and it will be the parent node for the remaining nodes:

$one = $root->new_child("Travelpackage", "");
$one->setattr("name", "c");

Next is the array with the values to be added to the document:

$nodeName = array(
    "Country_name" => "Jamacia",
    "City" => "Ocho Rios",
    "Resort" => "Sandles Ocho Rios",
    "Resort_rating" => "3",
    "Resort_typeofholiday" => "beach",
    "Resort_watersports" => "true",
    "Resort_meals" => "true",
    "Resort_drinks" => "true");

Add the values using a while statement:

    while (list($key,$value) = each($nodeName)) { $one-
    >new_child($key, $value); }

The DOM Model Page 33
Professional PHP4 Programming
The DOM Model Page 35


Up to => Home / Authoring / Languages / PHP / Pro




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers