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 35

March 15, 2002


Add the package details:

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

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

We then write all the data back into an XML file. We could write this back into a different file by changing the name of the file in the fopen() statement. We are using the w+ mode when appending to an XML document, which seems counter-intuitive. If we were to use the a+ mode, we'd end up with an XML file that has a total of five sets of Travelpackage elements and two roots. We've already got two Travelpackage elements in the file, we read them into memory, add another, and then write to the end of the file creating 2 + 2 + 1. This will also create an invalid XML file, as there will be two XML declarations.

Next we write the contents of the XML object $doc which is stored in memory into the file travel.xml:

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

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

We end up the following XML file:

<?xml version="1.0"?>
<Recordset>
  <Travelpackage name="a">
    <Country_name>Cuba</Country_name>
    <City>Cayo Coco</City>
    <Resort>Club Tryp Cayo Coco</Resort>
    <Resort_rating>4</Resort_rating>
    <Resort_typeofholiday>beach</Resort_typeofholiday>
    <Resort_watersports>true</Resort_watersports>
    <Resort_meals>true</Resort_meals>
    <Resort_drinks>true</Resort_drinks>
    <Package>
      <Package_dateofdep>5/8/89</Package_dateofdep>
      <Package_price>879</Package_price>
    </Package>
  </Travelpackage>
  <Travelpackage name="b">
    <Country_name>Cuba</Country_name>
    <City>Varadero</City>
    <Resort>Sol Club Paleras</Resort>
    <Resort_rating>3</Resort_rating>
    <Resort_typeofholiday>beach</Resort_typeofholiday>
    <Resort_watersports>false</Resort_watersports>
    <Resort_meals>true</Resort_meals>
    <Resort_drinks>false</Resort_drinks>
    <Package>
      <Package_dateofdep>5/1/89</Package_dateofdep>
      <Package_price>779</Package_price>
    </Package>
  </Travelpackage>
  <Travelpackage name="c">
    <Country_name>Jamacia</Country_name>
    <City>Ocho Rios</City>
    <Resort>Sandles Ocho Rios</Resort>
    <Resort_rating>3</Resort_rating>
    <Resort_typeofholiday>beach</Resort_typeofholiday>
    <Resort_watersports>true</Resort_watersports>
    <Resort_meals>true</Resort_meals>
    <Resort_drinks>true</Resort_drinks>
    <Package>
      <Package_dateofdep>5/11/89</Package_dateofdep>
      <Package_price>679</Package_price>
    </Package>
  </Travelpackage>
</Recordset>

XPath

Reading the objects out of the tree in the format that we need requires the use of XPath. So before we look at the code for reading the XML file with DOM we need to take a look at XPath.

XPath provides a syntax for locating nodes in an XML document tree. XPath always operates on a context node, which is similar to the current directory in a UNIX shell, so it depends on where the XPath expression is executed. XPath is used to locate or select one or multiple nodes in an XML document.

So for example, the XPath expression Recordset will select all nodes with the element name Recordset. If we wanted to select the Travelpackage elements in all Recordset elements we would use the following XPath - Recordset/Travelpackages. This XPath expression would require Travelpackages to be an immediate descendant of Recordset. If we want to allow for other elements in-between, we would specify: Recordset//Travelpackages.

The root node is represented by /, just as in UNIX file systems. So, if we want to find immediate children of the root element we could specify /Recordset. The wildcard * matches any given name. So /* will select all children of the root element.

As there are different kinds of elements or pieces to an XML document, there are different types of nodes within XPath, each corresponding to the different parts of an XML document. The XPath element node is used to represent a tag, or element, of an XML document. For example, <Recordset> and <City> are both element nodes. The data within <City>Cayo Coco</City> is a text node.

The other node in the example XML is an attribute node which corresponds to the name= in the <Travelpackage name="a">. There are other nodes which are used to describe the other parts of an XML document like namespace and processing instructions, but we're not using them in this chapter so we won't go into detail. There are a couple of rules for nodes:

  • Every XML document has a root node, which generally corresponds to the highest level parent within an XML document.
  • Every node except the root node must have a parent node
  • Every node can have zero or more child nodes

The DOM Model Page 34
Professional PHP4 Programming
The DOM Model Page 36


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