Parsing the ExpiryData file


12/10/04
 

Parsing the ExpiryData file

Now for some PHP to parse the XML file; I used the "parsemoldb.php" example from this web page as my starting point. (It looks tricky - I don't fully understand how it works yet!)
 
<?php

/* Based on: http://jp2.php.net/xml_parse_into_struct */

//----------------------------------------------------------------
class ExpiredPage {
  var $url;
  var $owner;
  var $expired;
  var $message;

  function ExpiredPage ($aa) {
    foreach ($aa as $k=>$v)
      $this->$k = $aa[$k];
  }
}
//----------------------------------------------------------------
function readDatabase($fileurl) {
  // read the xml database of ExpiredPages
  $data = implode("",file($fileurl));
  $parser = xml_parser_create();
  xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
  xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
  xml_parse_into_struct($parser,$data,$values,$tags);
  xml_parser_free($parser);

  // loop through the structures
  foreach ($tags as $key=>$val) {
    if ($key == "expiredpage") {
      $molranges = $val;
      // each contiguous pair of array entries are the 
      // lower and upper range for each expiredpage definition
      for ($i=0; $i < count($molranges); $i+=2) {
        $offset = $molranges[$i] + 1;
        $len = $molranges[$i + 1] - $offset;
        $tdb[] = parseMol(array_slice($values, $offset, $len));
      }
    } else {
      continue;
    }
  }
  return $tdb;
}
//----------------------------------------------------------------
function parseMol($mvalues) {
  for ($i=0; $i < count($mvalues); $i++)
    $mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
  return new ExpiredPage($mol);
}
//----------------------------------------------------------------
/*
// MAIN PROGRAM
*/

$fileurl = "ExpiryData.xml";

echo "<html><head><title></title></head><body><pre>";

$db = readDatabase($fileurl);
print_r($db);

echo "</pre></body></html>";

?>

 

This works nicely; it produces this output...

 
Array
(
    [0] => expiredpage Object
        (
            [url] => http://www.lsbu.ac.uk/1-etc.etc
            [owner] => martin.bush@lsbu.ac.uk
            [expired] => 2004-10-12
            [message] => example message 1
        )

    [1] => expiredpage Object
        (
            [url] => http://www.lsbu.ac.uk/2-etc.etc
            [owner] => mike.child@lsbu.ac.uk
            [expired] => 2004-10-13
            [message] => example message 2
        )

    [2] => expiredpage Object
        (
            [url] => http://www.lsbu.ac.uk/3-etc.etc
            [owner] => martin.bush@lsbu.ac.uk
            [expired] => 2004-10-14
            [message] => example message 3
        )

)
 

 
 

<<contents ^top^