Name: | Your Name |
Course: | CMPS 3500 - Programming Languages |
Lab #12: | PHP XML Parser |
";
report();
//findbyregex("/[lL]inux/");
if (strlen($regex) > 0) {
findbyregex($regex);
}
$debug = 1;
if ($debug) {
dump_array();
}
echo "
";
//----------------------------------------------------------------------
// Setup variables, open file, create a parser object and event handlers.
//----------------------------------------------------------------------
function parse($filename)
{
global $n_items;
$n_items = 0;
$xml_parser = xml_parser_create();
// set handler for start and end tags
xml_set_element_handler($xml_parser, "XML_startElement","XML_endElement");
// set handler for character data between tags
xml_set_character_data_handler($xml_parser, "XML_data");
if( !($fp = fopen($filename, 'r')) ) {
die("could not open XML input");
}
// read 4096 bytes at a time
while($data = fread($fp, 4096)) {
// parse the file
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)));
}
}
xml_parser_free($xml_parser);
}
//--------------------------------------------------------------------
// Specify what to do with each parsed token; i.e. the token handler.
//--------------------------------------------------------------------
function XML_startElement($parser, $name, $attrs = '') {
global $n_items;
global $software;
switch($name) {
case 'SOFTWARE':
$software= array();
break;
case 'ITEM':
$software[$n_items] = array();
foreach($attrs as $key => $value) {
$software[$n_items][strtolower($key)] = $value;
}
$n_items++;
break;
}
}
//----------------------------------------------------------------------------
// Function to handle the element's character data.
// or: perform regex replacement
// $char_data = preg_replace('/ */',' ',$data);
//----------------------------------------------------------------------------
function XML_data($parser, $data) {
global $n_items;
$index = $n_items - 1;
global $software;
$char_data = trim($data);
$key = "cdata";
if($char_data) {
$software[$index][$key] = $char_data;
}
}
//---------------------------------------------------------------------------
// For debugging dump everything.
//---------------------------------------------------------------------------
function dump_array() {
global $n_items;
global $software;
// a var_dump function
echo "DEBUGGING DUMP\n";
echo substr(date('r'),0,16);
print_r($software);
printf('number of items: %d',$n_items);
}
//--------------------------------------------------------------------------
// This function must be present even if empty.
//--------------------------------------------------------------------------
function XML_endElement($parser, $name) {
}
//----------------------------------------------------------------------------
// Function to query XML table by pattern string.
//----------------------------------------------------------------------------
function findbyregex($pattern) {
$cnt = 0;
global $n_items;
global $software;
printf("\nNames of matches for %s:\n",$pattern);
$i = 0;
while ($i < $n_items){
$category = $software[$i]["category"];
$description = $software[$i]["description"];
$name = $software[$i]["name"];
// preg_match returns true if part or entire string matches pattern
if (preg_match($pattern,$category)) {
printf("%s\n",$name);
$i++;
$cnt++;
continue;
}
if (preg_match($pattern,$description)) {
printf("%s\n",$name);
$i++;
$cnt++;
continue;
}
$i++;
}
printf("Records matched: %d \n",$cnt);
}
//-----------------------------------------------------------------------
// Display a tabular list of item name, item description and item count.
//-----------------------------------------------------------------------
function report() {
global $n_items;
global $software;
$td = substr(date('r'),0,16);
printf("SOFTWARE REPORT\nDate: %s\n",$td);
printf("\n");
$format = "%-4s%-12s%-22s%-25s%-15s";
printf($format,"ID ","NAME","DESCRIPTION","CATEGORY", "NOTES");
//printf("\n");
//printf($format,"---","-----","------------","----------","-----");
printf("\n");
$i = 0;
while ($i < $n_items){
$id = $software[$i][ "id" ];
$nm = $software[$i][ "name" ];
$dsc = $software[$i][ "description" ];
$cat = $software[$i][ "category" ];
$cnt = $software[$i][ "cdata" ];
printf($format, $id, $nm, $dsc, $cat, $cnt);
printf("\n");
$i += 1;
}
printf("\nRECORDS PROCESSED: %d\n",$i);
echo "report generated by Your Name\n";
}
?>