<?php
//*=============================================================*//
//* *//
//* XMLプログラム *//
//* *//
// http://www.geocities.jp/xmlfirststep/pxml/pxml_menu.html *//
// を参考にしました *//
//*=============================================================*//
// 保存領域
$no = -1;
//======================================//
// テキスト部分の処理 //
//======================================//
function characters($parser, $text)
{
global $no;
if ( $no > 0 )
{
print "${text}<BR>¥n";
}
}
//======================================//
// 要素の開始 //
//======================================//
function startElement($parser, $name, $attrib)
{
global $no;
// 出力する場合、noを1以上に
if($name == 'link')
{
if ( $no >= 0 )
{
print "link:";
$no = 1;
}
}
elseif ($name == 'title')
{
if ( $no >= 0 )
{
print "title:";
$no = 2;
}
}
elseif ($name == 'item')
{
$no = 0;
}
else
{
$no = -1;
}
}
//======================================//
// 要素の終了 //
//======================================//
function endElement($parser, $name)
{
// 今回は、なにもすることはない
}
//======================================//
// 実際の処理 //
//======================================//
function readXML($file)
{
//XMLパーサー作成("UTF-8")
$xml_parser = xml_parser_create("UTF-8");
// 大文字変換を行わない
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false);
//start および end 要素のハンドラを設定する
xml_set_element_handler($xml_parser, "startElement", "endElement");
//文字データハンドラを設定する
xml_set_character_data_handler($xml_parser, "characters");
if (!($fp = fopen($file, "r")))
{
die("XMLファイルを開けません。");
}
while ($data = fread($fp, 4096))
{
//XMLパース処理
if (!xml_parse($xml_parser, $data, feof($fp)))
{
//パースエラー処理
die(printf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
//XMLパーサの開放
xml_parser_free($xml_parser);
}
//処理実行
readXML("test.xml");
?>
|