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 = 単一のフィールド,2 = 配列型のフィールド,3 = レコードのコンテナ $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); if ($this->ends_record[$element]) { $this->records[] = $this->record; $this->record = array(); } $this->current_field = ''; } function cdata ($p, $text) { if ($this->field_type[$this->current_field] === 2) { $this->record[$this->current_field][] = $text; } elseif ($this->field_type[$this->current_field] === 1){ $this->record[$this->current_field] .= $text; } } function show_menu() { echo "\n"; foreach ($this->records as $book) { echo ""; $authors = join(', ', $book['author']); printf("\n", $_SERVER['PHP_SELF'] . '?isbn=' . $book['isbn'], $book['title'], $authors); echo "\n"; } } function show_book ($isbn) { foreach ($this->records as $book) { if ($book['isbn'] !== $isbn) { continue; } $authors = join(', ', $book['author']); printf("%s by %s.
", $book['titile'], $authors); printf("コメント: %s

\n", $book['comment']); } ?> 一覧に戻る

show_book($_GET['isbn']); } else { // メニューを返します $my_library->show_menu(); } ?>

%s%s