By separating content from presentation, XML offers Web developers a powerful alternative to traditional HTML technology...and when you combine that with PHP, you have a truly compelling new set of tools. In this article, find out how PHP's SAX parser can be used to parse XML data and generate HTML Web pages.
Here's another, slightly more complex example using the SAX parser, and one of my favourite meals.
<?xml version="1.0"?>
<recipe>
<name>Chicken Tikka</name>
<author>Anonymous</author>
<date>1
June 1999</date>
<ingredients>
<item>
<desc>Boneless chicken
breasts</desc>
<quantity>2</quantity>
</item>
<item>
<desc>Chopped
onions</desc>
<quantity>2</quantity>
</item>
<item>
<desc>Ginger</desc>
<quantity>1
tsp</quantity>
</item>
<item>
<desc>Garlic</desc>
<quantity>1
tsp</quantity>
</item>
<item>
<desc>Red chili powder</desc>
<quantity>1
tsp</quantity>
</item>
<item>
<desc>Coriander seeds</desc>
<quantity>1
tsp</quantity>
</item>
<item>
<desc>Lime juice</desc>
<quantity>2
tbsp</quantity>
</item>
<item>
<desc>Butter</desc>
<quantity>1
tbsp</quantity>
</item>
</ingredients>
<servings>
3
</servings>
<process>
<step>Cut
chicken into cubes, wash and apply lime juice and salt</step>
<step>Add
ginger, garlic, chili, coriander and lime juice in a separate
bowl</step>
<step>Mix
well, and add chicken to marinate for 3-4 hours</step>
<step>Place chicken
pieces on skewers and barbeque</step>
<step>Remove, apply butter, and
barbeque again until meat is tender</step>
<step>Garnish with lemon and
chopped onions</step>
</process>
</recipe>
This time, my PHP script won't be using a "switch" statement when I parse the
file above; instead, I'm going to be keying tag names to values in a hash. Each of the tags in the XML file above will be replaced with appropriate HTML markup.
<html>
<head>
</head>
<body bgcolor="white">
<?
// data file
$file
= "recipe.xml";
/*
hash of tag names mapped to HTML markup
"RECIPE" => start
a new block
"NAME" => in bold
"INGREDIENTS" => unordered list
"DESC" => list items
"PROCESS"
=> ordered list
"STEP" => list items
*/
$startTags = array(
"RECIPE" => "<hr>",
"NAME"
=> "<font size=+2>",
"DATE" => "<i>(",
"AUTHOR" => "<b>",
"SERVINGS"
=> "<i>Serves ",
"INGREDIENTS" => "<h3>Ingredients:</h3><ul>",
"DESC"
=> "<li>",
"QUANTITY" => "(",
"PROCESS" => "<h3>Preparation:</h3><ol>",
"STEP"
=> "<li>"
);
// close tags opened above
$endTags = array(
"NAME" => "</font><br>",
"DATE"
=> ")</i>",
"AUTHOR" => "</b>",
"INGREDIENTS" => "</ul>",
"QUANTITY"
=> ")",
"SERVINGS" => "</i>",
"PROCESS" => "</ol>",
);
function startElement($parser,
$name, $attrs) {
global $startTags;
// if tag exists as key, print value
if
($startTags[$name]) { echo $startTags[$name]; }
}
function endElement($parser,
$name) {
global $endTags;
if ($endTags[$name]) { echo $endTags[$name]; }
}
//
process data between tags
function characterData($parser, $data) {
echo $data;
}
//
initialize parser
$xml_parser = xml_parser_create();
// set callback functions
xml_set_element_handler($xml_parser,
"startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
//
open XML file
if (!($fp = fopen($file, "r")))
{
die("Cannot locate XML data
file: $file");
}
// read and parse data
while ($data = fread($fp, 4096))
{
// error handler
if (!xml_parse($xml_parser, $data, feof($fp)))
{
die(sprintf("XML
error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
// clean up
xml_parser_free($xml_parser);
?>
</body>
</html>
In this case, I've set up two hashes (associative arrays), one for opening tags
and one for closing tags. When the parse encounters an XML tag, it looks up the array to see if the tag exists as a key. If it does, the corresponding value (HTML markup) is printed. This method does away with the slightly cumbersome "switch" statements of the previous example, and is easier to read and understand.
Here's the output:
That's about it for the moment. Over the last few pages, I've discussed using PHP's built-in SAX parser to process an XML file and mark up the data within it with HTML tags. However, just as there's more than one way to skin a cat, there's more than one way to process XML data in PHP. In the second part of this article, I'll be looking at an alternative technique of parsing an XML file, this time using the DOM. Make sure you come back for that one!