Page 338 - Open Soource Technologies 304.indd
P. 338
Web Technologies-I
Notes </head>
<body>
<?php
class BookList
{
var $parser;
var $record;
var $current_field = ‘’;
var $field_type;
var $ends_record;
var $records;
function BookList ($filename)
{
$this->parser = xml_parser_create( );
xml_set_object($this->parser, &$this);
xml_set_element_handler($this->parser,’start_element’, ‘end_element’);
xml_set_character_data_handler($this->parser, ‘cdata’); // 1 = single field, 2 = array field, 3 =
record container
$this->field_type = array(‘title’ => 1, ‘author’ => 2, ‘isbn’ => 1, ‘comment’ => 1);
$this->ends_record = array(‘book’ => true);
$x = join(“”, file($filename)); xml_parse($this->parser, $x);
xml_parser_free($this->parser);
}
function start_element ($p, $element, &$attributes) {
$element = strtolower($element);
if ($this->field_type[$element] != 0)
{
$this->current_field = $element;
}
else {
$this->current_field = ‘’;
}
}
function end_element ($p, $element) {
$element = strtolower($element);
332 LOVELY PROFESSIONAL UNIVERSITY