The RAX Model Page 39
March 15, 2002
The RAX model works with record-oriented XML documents and SML
style XML documents. The PHP implementation of RAX is called
PRAX, and should be considered alpha or pre-alpha code.
Currently PRAX is the only implementation of RAX available for
PHP. PRAX is a port of the perl module XML::RAX. It is used to
read the contents of an XML file, but does not support writing
XML documents.
You can get the source for PRAX at
http://www.oreillynet.com/~rael/lang/php/PRAX/.
Using PHP's PRAX Support
PRAX can be used to interact with an XML document in much the
same way we would interact with a SQL query. A SQL query returns
a recordset that we can manipulate or display. PRAX behaves the
same way and was designed to work with data this way.
There are two ways to work with PRAX - we can either send a
string of XML to PRAX, or use it to open an XML file. In the
following example we will open a file from the file system.
PRAX Example Code
The following code is modified from the example code provided by
PRAX author Rael Dornfest.
This chapter makes minor changes to the PRAX.php class. The
example code by Rael dumped everything out to the screen, which
we don't want to happen. We'll use a global debug. The author
has been notified, but if the code is not updated you will need
to update the class yourself or download it from
http://www.wrox.com/. Here's what you need to do:
- Open up PRAX.php with a text editor
- Find the line: $this->debug = 1;
- Change this line so it looks like: $this->
debug = $GLOBALS["debug"];
For this example, we need to have two files in the
same directory:
- travel_simple.xml
- travel_sample.php
Recall that travel.xml has attributes, and somewhat complex
nestings. If we include attributes like name="a", then the
attribute is ignored by PRAX. If our XML document has more than
two levels of nesting then the contents of the nested elements
are combined.
For example, our XML file has the element Package which is the
parent of Package_dateofdep and Package_price. The values placed
into these two children elements are combined to create a single
array. In our example the array contains "5/8/98 879" and
"5/8/98 779".
If you want to use PRAX make sure that you don't use more than
three levels of nesting. Therefore, in the travel.xml file
either remove the <Package>...</Package> elements or
change the elements so they're all on the same level:
<Resort_drinks>false</Resort_drinks>
<Package_dateofdep>5/1/98</Package_dateofdep>
<Package_price>779</Package_price>
Said another way, make sure you're XML document is in SML format
before you use PRAX. The modified version of travel.xml is
called travel_simple.xml.
The code file, travel_sample.php loads, reads, and displays the
contents of the XML file as an HTML table:
<?php
# Set debug to 0 if you don't want to see all the processing
information on the screen
# or to 1 if you do want to see all the processing information on
the screen
$debug = "1";
global $debug;
?>
When you've got this working and you're happy with the HTML
formatting you can turn off all the processing information by
changing the debug value to 0:
<html>
<head>
<title>PRAX Demonstration</title>
</head>
<body>
<?php
print("<h1>Travel Packages</h1>\n");
As long as the path to PRAX.php is valid this code
will work:
# Include the RAX library
include("./PRAX.php");
Next we instantiate the RAX object:
# Create a new RAX object
$rax = new RAX();
Then we load the XML file:
# Open the XML document
$rax->openfile("./travel.xml");
The DOM Model Page 38
Professional PHP4 Programming
The RAX Model Page 40
|