Generating XML from a recordset is very similar to creating any other web page that dis- plays content drawn from a database. The main difference is that you replace all the HTML tags with XML tags. Before getting down to the detail, here’s a brief outline of the steps involved:
1.Create a recordset.
2.Build a skeleton of XML tags for the repeating element and its child nodes.
3.Populate the child nodes with dynamic text objects from the recordset.
4.Apply a Repeat Region server behavior to the repeating element.
5.Remove the HTML code from the page.
6.Add the root node tags.
7.Add headers and the XML declaration to tell Dreamweaver and browsers to treat the output as XML.
You build everything like an ordinary web page and remove the DOCTYPEdeclaration and HTML tags, leaving behind just the code to create the XML feed. However, it’s important to leave all the HTML code in the page until you have applied the Repeat Region server behavior. Otherwise, Dreamweaver cannot recognize where to insert the code and refuses to apply the server behavior.
The following instructions show you how to create an XML feed of details of the Japanese images in the ch19_gallerytable:
1.Create a new PHP page called japan_xml.phpinworkfiles/appendix.
2.Open the Recordset dialog box in Simple mode, and create a recordset called getPhotos. This doesn’t need administrative privileges, so use connQuery for Connection, and select ch19_galleryin the Tablefield.
You don’t need the photo_idand categorycolumns for the XML output, so select all other columns except those two.
However, you do want to retrieve only those records where category is set to JPN. Set Filterto category, and leave the second drop- down set to =. Since JPN is a fixed value, select Entered Value from the third drop- down, and type JPNin the field alongside.
When you have finished, the settings should look like this:
A
3.Use the Testbutton to make sure the record- set works, and then click OKto save it.
4.Now build a skeleton for the repeating ele- ment, <photo>, and its child nodes. You need just one set of tags, because the repeat region generates the rest. Switch to Code
view, and insert the following code between the <body>tags:
<body>
<photo>
<filename></filename>
<width></width>
<height></height>
<caption></caption>
<description><![CDATA[]]></description>
</photo>
</body>
The <description> node contains HTML, so I have added opening and closing CDATAtags inside the node tags (CDATAsections instruct XML parsers to treat their content as plain character data). When building your own XML, create a similar skeleton using the node names of your choice.
As you’re typing, you’ll notice that Dreamweaver code hints recognize your custom XML tags, making it easier to complete the closing tags.
5.Now populate the child nodes with dynamic text objects from the recordset.
Position the insertion point between the opening and closing <filename>tags.
You need to insert the XML skeleton between the <body>tags in order to use the Repeat Region server behavior.
6.Open the Bindingspanel, expand the recordset, select filename, and click the Insert button. This inserts a dynamic text object inside the <filename>child node.
7.Repeat steps 5 and 6 with the other child node tags, positioning the insertion point between the CDATA tags for the <description> node. The XML skeleton should now look like this:
<photo>
<filename><?php echo $row_getPhotos['filename']; ?></filename>
<width><?php echo $row_getPhotos['width']; ?></width>
<height><?php echo $row_getPhotos['height']; ?></height>
<caption><?php echo $row_getPhotos['caption']; ?></caption>
<description><![CDATA[<?php echo $row_getPhotos['description']; ?> ➥ ]]></description>
</photo>
8.Select the XML skeleton from the opening <photo> tag to the closing </photo>
one, and apply a Repeat Region server behavior (use the Server Behaviorspanel, the Datatab of the Insert bar, or the Insert ➤ Data Objectssubmenu). In the Repeat Regiondialog box, select Show All Records.
9.Once the Repeat Region server behavior has been applied, you can get rid of the unwanted HTML. Select everything from the opening tag of the DOCTYPEdeclara- tion to the opening PHP tag at the start of the repeat region that you have just cre- ated, as shown in the following screenshot:
10.Delete the selected code, and replace it with the opening tag of the XML root node like this:
$totalRows_getPhotos = mysql_num_rows($getPhotos);
?>
<gallery>
<?php do { ?>
11.Scroll down and replace the closing </body>and </html>tags with the closing tag of the XML root node (</gallery>).
12.The last change you need to make is to insert headers and the XML declaration to tell Dreamweaver and browsers to treat the output as XML. Without them, they treat it as plain text. The headers go just before the closing PHP tag shown on line 39 of the preceding screenshot, like this:
$totalRows_getPhotos = mysql_num_rows($getPhotos);
// Send the headers
header('Content-type: text/xml');
header('Pragma: public');
header('Cache-control: private');
header('Expires: -1');
// Add the XML declaration
echo '<?xml version="1.0" encoding="utf-8"?>';
?>
<gallery>
13.Save japan_xml.php, and test the page in a browser. It should look the same as Figure A-1. You can compare your code with japan_xml.phpin examples/appendix.
Forgetting the headers is a common cause of problems when generating XML on the fly. Since you’re using a file with a .phpextension, the web server doesn’t know that it’s meant to treat the output as XML without sending the Content- typeheader. The remaining three headers are optional but are designed to pre- vent the XML output from being cached. The XML declaration isn’t strictly necessary, but it’s usual to include it. I have used echoto insert the XML decla- ration to avoid conflicts with servers that permit the short opening PHP tag (<?).
A
Not only is generating XML dynamically like this a lot easier than typing out everything laboriously in an XML document, but it also has the advantage that the XML data source is automatically updated each time you make any changes to the records in the database.