The RAX Model Page 40
March 15, 2002
We define which tag is the delimiter or separator for the different rows. The value of this delimiter will be the same as the name of the second level element in the XML document. In this example it's Travelpackage:
# Select the individual record delimiter, similar table row
$rax->record_delim = 'Travelpackage';
# Start parsing the XML document $rax->parse();
# Read the first record $rec = $rax->readRecord();
Next, we can display the contents of the XML document any way we
want. To anyone who's worked with displaying database queries in
HTML this will be very familiar. Instead of connecting to a
database, running a query, and returning the results, PRAX runs
the getRow() function. This code will cycle through the XML
document, using the while ($rec) as long as there are recordsets
to display. The contents of the XML document are put into the
$row as an array. The fieldnames are the keys and the fields are
the values or the array. In this example the code displays the
XML as a table with two columns:
echo("<table cellpadding=\"0\" border=\"0\">\n");
while ( $rec ) {
$row = $rec->getRow();
echo("<tr><td>Country_name</td><td>" .
$row["Country_name"] . "</td></tr>\n");
echo("<tr><td>City</td><td>" . $row["City"] . "</td></tr>\n");
echo("<tr><td>Resort</td><td>" . $row["Resort"] . "</td></tr>\n");
echo("<tr><td>Resort_rating</td><td>" .
$row["Resort_rating"] . "</td></tr>\n");
echo("<tr><td>Resort_typeofholiday</td><td>" .
$row["Resort_typeofholiday"] . "</td></tr>\n");
echo("<tr><td>Resort_watersports</td><td>" .
$row["Resort_watersports"] . "</td></tr>\n");
echo("<tr><td>Resort_meals</td><td>" .
$row["Resort_meals"] . "</td></tr>\n");
echo("<tr><td>Resort_drinks</td><td>" .
$row["Resort_drinks"] . "</td></tr>\n");
echo("<tr><td colspan=2><hr></td></tr>\n");
$rec = $rax->readRecord();
}
echo("</table>\n");
?>
</body>
</html>
Notice that we have to define the row key in order to get the
values to display. This means that in its current form, PRAX
can't read an XML file and use the fieldnames to display the row
keys.
The following figure shows the output of the above code:
Displaying the XML Document
The following reads through the XML document and displays
the document as XML to the browser:
# Display XML
if ($debug=="1") {
print("<b>Given the XML:</b> <pre>" .
htmlentities(implode("", file("./travel.xml"))) . "</pre>");
}
The html entities() function currently only escapes the ISO-
8859-1 (or ISO-latin-1) character set, so it may not work
properly with some international XML documents.
There are two main parts to the travel.xml document. First are
the element names, which are roughly the same as field names of
a table in a database. Second are the values of the elements
which are the same as the contents of a record in a table. If
you have got debugging on then you'll see the column names
(referred to as Fieldnames) and you'll see the values in the
cells (or Fields):
# Field Names
if ($debug=="1") {
$fieldnames = $rec->getFieldnames();
print("<b>\$rec->getFieldnames()</b>" . "<blockquote>" .
join("<br />", $fieldnames) . "</blockquote>");
}
# Field Values
if ($debug=="1") {
print("<b>\$rec->getFields()</b>" . "<blockquote>" .
join("<br />", $rec->getFields()) . "</blockquote>");
}
Thus, if this code were added to the travel_sample.php
file, then the output of the code would be:
The RAX Model Page 39
Professional PHP4 Programming
XSL and XSLT Page 41
|