file: movies.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- Creation date: 8/25/2006 -->
<movies>
<movie>
<movieName>We Were Soldiers</movieName>
<movieNote href="http://www.hollywood.com">Vietnam movie staring <b>Mel Gibson</b></movieNote>
<movieType>Action</movieType>
<movieId>99</movieId>
<show>
<showNo>1</showNo>
<showDate>3-1-2002</showDate>
<showTime>21.00</showTime>
<freeseats>10</freeseats>
</show>
</movie>
<movie>
<movieName>Shrek</movieName>
<movieNote href="http://www.shrek.com">Funny cartoon about <b>Shrek and Friends.</b></movieNote>
<movieType>Cartoon</movieType>
<movieId>100</movieId>
<show>
<showNo>1</showNo>
<showDate>4-8-2002</showDate>
<showTime>19.00</showTime>
<freeseats>0</freeseats>
</show>
file: show.php
<?php
$xmlSource = "movies.xml";
$currentElement = "";
$shows = array();
$movieName = "";
$movieNote = "";
$movieType = "";
$movieId = "";
$showNo = "";
$showDate = "";
$showTime = "";
$freeseats = "";
$href = "";
function startElement($parser, $name, $attr)
{
$GLOBALS['currentElement'] = $name;
/* if the element is movieNote, we want the value of the href attribute */
if ($name == "movieNote") {
$GLOBALS['href'] = $attr["href"];
}
} /* end startElement() */
function endElement($parser, $name)
{
$elements = array('movieName', 'movieNote', 'movieType', 'movieId',
'showNo', 'showDate', 'showTime', 'freeseats', 'href');
/* If the element being parsed is a show it means that the parser has
* completed parsing show. We can then store the data in our array
* $shows[]
*/
if ($name == "show") {
foreach ($elements as $element) {
$temp[$element] = $GLOBALS[$element];
}
$GLOBALS['shows'][] = $temp;
/* After storing the data we reset our global show-variables to hold a
* new show */
$GLOBALS['showNo'] = "";
$GLOBALS['showDate'] = "";
$GLOBALS['showTime'] = "";
$GLOBALS['freeseats'] = "";
}
/* After parsing a movie we reset the rest of the globals.*/
if (strcmp($name, "movie") == 0) {
$GLOBALS['movieName'] = "";
$GLOBALS['movieNote'] = "";
$GLOBALS['movieType'] = "";
$GLOBALS['movieId'] = "";
$GLOBALS['href'] = "";
}
} /* end endElement() */
function characterData($parser, $data)
{
$elements = array (
'movieName', 'movieNote', 'movieType',
'movieId', 'showNo', 'showDate', 'showTime', 'freeseats'
);
foreach ($elements as $element) {
if ($GLOBALS ['currentElement'] == $element) {
$GLOBALS[$element] .= $data;
}
}
}
function parseFile()
{
global $xmlSource, $shows;
/* Creating the xml parser */
$xml_parser = xml_parser_create();
/* Register the handlers */
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
/* Disabling case folding. Case-folding basically means that XML, by
* default, will send all the element names, to the parser, as
* uppercase. Since it is case-sensitive we need to disable that in
* this example. */
xml_parser_set_option ($xml_parser, XML_OPTION_CASE_FOLDING, FALSE);
/* Open the xml file and pass it to the parser in 4k chunks. Return
* an error if file cannot be opened. */
if (!($fp = fopen($xmlSource,"r"))) {
die("Cannot open $xmlSource.");
}
while (($data = fread($fp,4096))) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die (sprintf("XML error at line %d column %d ",
xml_get_current_line_number($xml_parser),
xml_get_current_column_number($xml_parser)));
}
}
/* Finish ! we free the parser and returns the array */
xml_parser_free($xml_parser);
return $shows;
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<table border="1">
<?php
$result = parseFile();
foreach ($result as $arr) {
/* check on movieId to see if we reached a new movie. If so we print out
* the movieName */
if (strcmp($id, $arr["movieId"]) != 0) {
$data = "
<tr>
<td colspan=\"3\"><b>{$arr[movieName]}</b></td>
</tr>
<tr>
<td colspan=\"3\">{$arr[movieNote]}</td>
</tr>
<tr>
<td colspan=\"3\"><a href={$arr[href]}>visit website</a></td>
</tr>";
echo $data;
}
$data = "
<tr>
<td>Date: {$arr[showDate]}</td>
<td>Time: {$arr[showTime]}</td>
<td>Seats : {$arr[freeseats]}</td>
</tr> ";
echo $data;
$id = $arr["movieId"];
}
?>
</table>
Here problem: Script stopped were occur "<b>" tag!