独自のXMLハンドラを使用して解析1
概要
独自のハンドラを使用して解析
サンプル
XML
<?xml version="1.0" encoding="utf-8"?>
<library>
<book>
<title>Programming PHP</title>
<authors>
<author>Rasmus Lerdorf</author>
<author>Kevin Tatroe</author>
<author>Peter MacIntyre</author>
</authors>
<isbn>1-56592-610-1</isbn>
<comment>すばらしい本です!</comment>
</book>
<book>
<title>PHP Pocket Reference</title>
<authors>
<author>Rasmus Lerdorf</author>
</authors>
<isbn>1-56592-769-9</isbn>
<comment>ほんとにポケットにはいっちゃいます</comment>
</book>
<book>
<title>Perl Cookbook</title>
<authors>
<author>Tom Christiansen</author>
<author>Nathan Torkington</author>
</authors>
<isbn>1-56592-243-3</isbn>
<comment>役に立つテクニックが満載されています.
Perl だけじゃなく PHP でも応用できるでしょう.
</comment>
</book>
</library>
PHP
<html>
<head><title>私の書庫</title></head>
<body>
<?php
class BookList {
private $parser;
private $record;
private $current_field = '';
private $field_type;
private $ends_record;
private $recotds;
function __construct($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 = 単一のフィールド,2 = 配列型のフィールド,3 = レコードのコンテナ
$this->field_type = array(
'library' => 0,
'title' => 1,
'author' => 2,
'isbn' => 1,
'comment' => 1,
'book' => 3,
'authors' => 3);
$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 (isset($this->ends_record[$element])) {
$this->records[] = $this->record;
$this->record = array();
}
$this->current_field = '';
}
function cdata ($p, $text) {
if($this->current_field != ""){
if ($this->field_type[$this->current_field] === 2) {
$this->record[$this->current_field][] = $text;
} elseif ($this->field_type[$this->current_field] === 1){
if(isset($this->record[$this->current_field])){
$this->record[$this->current_field] .= $text;
}else{
$this->record[$this->current_field] = $text;
}
}
}
}
function show_menu() {
echo "<table border=1>\n";
foreach ($this->records as $book) {
echo "<tr>";
$authors = join(', ', $book['author']);
printf("<th><a href='%s'>%s</a></th><td>%s</td></tr>\n",
$_SERVER['PHP_SELF'] . '?isbn=' . $book['isbn'],
$book['title'],
$authors);
echo "</tr>\n";
}
}
function show_book ($isbn) {
foreach ($this->records as $book) {
if ($book['isbn'] !== $isbn) {
continue;
}
$authors = join(', ', $book['author']);
printf("<b>%s</b> by %s.<br>", $book['title'], $authors);
printf("ISBN: %s<br>", $book['isbn']);
printf("コメント: %s<p>\n", $book['comment']);
}
?>
<a href="<?php echo $_SERVER['PHP_SELF'] ?>">一覧</a>に戻る<p>
<?php
}
}
?>
<?php
//
$my_library = new BookList("sample07_1.xml");
if (isset($_GET['isbn']) && $_GET['isbn'] != "") {
// 詳細情報を返します.
$my_library->show_book($_GET['isbn']);
} else {
// メニューを返します
$my_library->show_menu();
}
?>
</body>
</html>
最終更新:2012年08月23日 22:35