<span style="COLOR: #000000"><font face="Courier New">この例のphpファイルはxmlファイルの内容を読み込んで表示します<br><br>以下はphpファイルの内容です。phpファイルとslashdot.xmlを同じフォルダに置くだけです。 <br><br></font><span style="COLOR: #0000bb"><font face="Courier New"><?php <br/>$open_tags </FONT></SPAN><FONT face="Courier New"><SPAN style="COLOR: #007700">= array( <br/> </SPAN><SPAN style="COLOR: #dd0000">'STORY ' </SPAN><SPAN style="COLOR: #007700">=> >=> </SPAN><SPAN style="COLOR: #dd0000">'<URL>'</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">,<br/></SPAN><SPAN style="COLOR: #dd0000">'作者'</SPAN><SPAN style="COLOR: #007700">=> </font></span><span style="COLOR: #dd0000"></span>'<font face="Courier New"><span style="COLOR: #007700"><br></span><span style="COLOR: #dd0000">$close_tags </span><span style="COLOR: #007700"></span><span style="COLOR: #dd0000">= array( </span> </font><font face="Courier New">'STORY' <span style="COLOR: #007700"><br>=> </span><span style="COLOR: #dd0000">'</ストーリー>'</span><span style="COLOR: #007700"></span> </font>、<span style="COLOR: #dd0000"> <font face="Courier New"><br>'TITLE' </font></span>=> <font face="Courier New"><span style="COLOR: #007700">' <br></span><span style="COLOR: #0000bb"></span> 、</font> <font face="Courier New"><span style="COLOR: #007700">'URL' <br></span>=> <span style="COLOR: #dd0000"></span>'</URL>'<SPAN style="COLOR: #007700"> </SPAN><SPAN style="COLOR: #dd0000"></SPAN>、</FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">'著者'<br/></SPAN>=> <span style="COLOR: #dd0000"></span><span style="COLOR: #007700"></span>'</著者> <span style="COLOR: #dd0000"></span></font><font face="Courier New"> <span style="COLOR: #007700">); <br></span><span style="COLOR: #dd0000"></span><span style="COLOR: #007700">?></span><span style="COLOR: #dd0000"></span></font><font face="Courier New"> <span style="COLOR: #007700"><?php <br/></SPAN><SPAN style="COLOR: #dd0000"></SPAN><SPAN style="COLOR: #007700">//以下はデータを抽出するために定義された関数です: </SPAN></FONT>// 処理開始タグの属性は参照しますto <SPAN style="COLOR: #dd0000">// $attrs は多次元配列で、キー値は属性です。名前、値は属性の値です。<FONT face="Courier New"><br/></FONT>関数 </SPAN><SPAN style="COLOR: #007700">startElement <FONT face="Courier New"><br/>(</FONT></SPAN>$parser<FONT face="Courier New"><SPAN style="COLOR: #0000bb">, <br/> </SPAN>$name<br/></FONT>, <SPAN style="COLOR: #0000bb"><FONT face="Courier New">$attrs<br/></FONT>=</SPAN><FONT face="Courier New">''<SPAN style="COLOR: #ff8000"><br/><br/><br/>){ <br/> グローバル </SPAN><SPAN style="COLOR: #007700">$open_tags</SPAN><SPAN style="COLOR: #0000bb">, </SPAN><SPAN style="COLOR: #007700">$ temp</SPAN><SPAN style="COLOR: #0000bb">、</SPAN><SPAN style="COLOR: #007700">$current_tag</SPAN><SPAN style="COLOR: #0000bb"></SPAN> <SPAN style="COLOR: #007700">; </SPAN> <SPAN style="COLOR: #0000bb"></SPAN>$current_tag <SPAN style="COLOR: #007700"></SPAN>= <SPAN style="COLOR: #dd0000"> </SPAN>$name</FONT><FONT face="Courier New"><SPAN style="COLOR: #007700"><br/>; </SPAN> if (<SPAN style="COLOR: #0000bb"></SPAN>$format <SPAN style="COLOR: #007700"></SPAN>= <SPAN style="COLOR: #0000bb"></SPAN>$open_tags<SPAN style="COLOR: #007700"></SPAN>[<SPAN style="COLOR: #0000bb"></SPAN>$name</FONT><FONT face="Courier New"><SPAN style="COLOR: #007700"><br/>]) { </SPAN> switch(<SPAN style="COLOR: #0000bb"></SPAN>$name<SPAN style="COLOR: #007700"></SPAN><SPAN style="COLOR: #0000bb"><🎜) >){ </SPAN> case </FONT><FONT face="Courier New">'STORY'<SPAN style="COLOR: #007700"><br/></SPAN> <SPAN style="COLOR: #0000bb">: </SPAN> echo <SPAN style="COLOR: #007700"></SPAN>' 新しいストーリー: '<SPAN style="COLOR: #0000bb"></SPAN> <SPAN style="COLOR: #007700"></SPAN>; <SPAN style="COLOR: #0000bb"> ブレーク; </SPAN> デフォルト: </FONT> ブレーク;} <br/> } <br/>} <br/><br/></FONT></SPAN><FONT face="Courier New"><SPAN style="COLOR: #ff8000">// $current_tag は処理中のタグを示します。これは後でcharacterData 関数で使用します <br/>/ / <br/>// </STORY> マークに遭遇すると、すべての一時変数をフラッシュし、次のマーク <br></span></STORY>'</font></span><font face="Courier New"><span style="COLOR: #007700">、<br> </span><span style="COLOR: #dd0000">'TITLE' </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">'</TITLE>'</span></font><font face="Courier New"><span style="COLOR: #007700">、<br> </span><span style="COLOR: #dd0000">'URL' </span><span style="COLOR: #007700">=> </span><span style="COLOR: #dd0000">'</URL>'</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">、<br/></SPAN><SPAN style="COLOR: #dd0000">'著者'</SPAN><SPAN style="COLOR: #007700">=> </span></font><span style="COLOR: #dd0000"><font face="Courier New">'<br></font></span><span style="COLOR: #007700"><font face="Courier New">); <br></font></span><font face="Courier New"><span style="COLOR: #0000bb">?><br></span><br></font><span style="COLOR: #0000bb"><font face="Courier New"><?php <br/> </FONT></SPAN><FONT face="Courier New"><SPAN style="COLOR: #ff8000">//以下はデータを抽出するために定義された関数です: <br/><br/>// 開始タグの属性を処理します <br/>// $attrs はキー値を持つ多次元配列が属性名で、その値が属性 <br/></SPAN><SPAN style="COLOR: #007700">関数 </SPAN><SPAN style="COLOR: #0000bb">startElement</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$parser</SPAN><SPAN style="COLOR: #007700">, </SPAN><SPAN style="COLOR: #0000bb">$name</SPAN><SPAN style="COLOR: #007700">, </SPAN><SPAN style="COLOR: #0000bb">$attrs</SPAN><SPAN style="COLOR: #007700">=</SPAN><SPAN style="COLOR: #dd0000">''</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">) { <br/> グローバル </SPAN><SPAN style="COLOR: #0000bb">$open_tags</SPAN><SPAN style="COLOR: #007700">, </SPAN><SPAN style="COLOR: #0000bb">$temp</SPAN><SPAN style="COLOR: #007700">, </SPAN> <SPAN style="COLOR: #0000bb">$current_tag</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700"> <br/> </SPAN><SPAN style="COLOR: #0000bb">$current_tag </SPAN><SPAN style="COLOR: #007700">= </SPAN><SPAN style="COLOR: #0000bb">$name</SPAN></FONT>; <FONT face="Courier New"><SPAN style="COLOR: #007700">; <br/> if ( </SPAN><SPAN style="COLOR: #0000bb">$format </SPAN><SPAN style="COLOR: #007700">= </SPAN><SPAN style="COLOR: #0000bb">$open_tags</SPAN><SPAN style="COLOR: #007700">[</SPAN><SPAN style="COLOR: #0000bb">$name </SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700"> ]){ <br/> switch(</SPAN><SPAN style="COLOR: #0000bb">$name</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">){ <br/> case </SPAN><SPAN style="COLOR: #dd0000">'STORY'</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">: <br/> echo </SPAN><SPAN style="COLOR: #dd0000">'新しいストーリー: '</SPAN></FONT><SPAN style="COLOR: #007700"><FONT face="Courier New">; ; <br/> デフォルト: <br/> Break; <br/> } <br/> } <br/>} <br/><br/><br/></FONT></SPAN><FONT face="Courier New">// $current_tag が示すタグ処理中です。これは後でcharacterData関数で使用します。<SPAN style="COLOR: #ff8000">// <br/>// </STORY>マークに遭遇すると、すべての一時変数をフラッシュし、次のマークの操作を準備することがわかります<br/><br/></SPAN>関数 <SPAN style="COLOR: #007700"></SPAN>endElement<SPAN style="COLOR: #0000bb"></SPAN>(<SPAN style="COLOR: #007700"></SPAN>$parser<SPAN style="COLOR: #0000bb"></SPAN>, <SPAN style="COLOR: #007700"></SPAN>$name<SPAN style="COLOR: #0000bb"></SPAN>, <SPAN style="COLOR: #007700"></SPAN>$attrs<SPAN style="COLOR: #0000bb"></SPAN> =<SPAN style="COLOR: #007700"></SPAN>''<SPAN style="COLOR: #dd0000"></SPAN></FONT><FONT face="Courier New">){ <SPAN style="COLOR: #007700"> グローバル <br/></SPAN>$close_tags<SPAN style="COLOR: #0000bb"></SPAN>、<SPAN style="COLOR: #007700"></SPAN>$temp <SPAN style="COLOR: #0000bb"></SPAN>、<SPAN style="COLOR: #007700"></SPAN>$current_tag<SPAN style="COLOR: #0000bb"></SPAN></FONT><FONT face="Courier New">;if (</SPAN><SPAN style="COLOR: #0000bb">$format </SPAN><SPAN style="COLOR: #007700">= </SPAN><SPAN style="COLOR: #0000bb">$close_tags</SPAN><SPAN style="COLOR: #007700">[</SPAN><SPAN style="COLOR: #0000bb">$name</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">]){ <br/> switch(</SPAN><SPAN style="COLOR: #0000bb">$name</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">){ <br/> case </SPAN><SPAN style="COLOR: #dd0000">'STORY'</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">: <br/> </SPAN><SPAN style="COLOR: #0000bb">return_page</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$temp</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">); <br/></SPAN>$temp <SPAN style="COLOR: #0000bb"></SPAN>= <SPAN style="COLOR: #007700"></SPAN>''<SPAN style="COLOR: #dd0000"></SPAN></FONT><SPAN style="COLOR: #007700">; <FONT face="Courier New"> デフォルト: <br/> ブレーク; ; <br/> } <br/> } <br/>} <br/><br/><br/><br/></FONT></SPAN>// この関数に渡されるのは要素間のデータです <FONT face="Courier New">//たとえば、<TITLE>Title Here</TITLE> の場合、$data は 'Title Here' と同じです <span style="COLOR: #ff8000"><br><br>function </span><span style="COLOR: #007700">characterData</span><span style="COLOR: #0000bb">(</span> <span style="COLOR: #007700"> $parser</span><span style="COLOR: #0000bb">, </span><span style="COLOR: #007700">$data</span><span style="COLOR: #0000bb"></span></font>){ <font face="Courier New"> グローバル <span style="COLOR: #007700"><br>$current_tag</span><span style="COLOR: #0000bb">、</span><span style="COLOR: #007700">$temp</span><span style="COLOR: #0000bb">、</span><span style="COLOR: #007700">$catID</span><span style="COLOR: #0000bb"></span></font> switch(<font face="Courier New"><span style="COLOR: #007700">; $current_tag<br></span><span style="COLOR: #0000bb"></span>){ </font> case <font face="Courier New"><span style="COLOR: #007700">'TITLE'<br></span><span style="COLOR: #dd0000"></span>: </font> <font face="Courier New"><span style="COLOR: #007700">$temp <br></span>[<span style="COLOR: #0000bb"></span>'タイトル'<span style="COLOR: #007700"></span>] = <span style="COLOR: #dd0000"></span>$data<span style="COLOR: #007700"></span><span style="COLOR: #0000bb"></span>; > </font><font face="Courier New">$current_tag <span style="COLOR: #007700"><br>= </span><span style="COLOR: #0000bb"></span><span style="COLOR: #007700">; </span> case <span style="COLOR: #dd0000"></span>' URL'</font><font face="Courier New"><span style="COLOR: #007700"><br>: <br> </span><span style="COLOR: #dd0000">$temp</span></font>[<font face="Courier New"><span style="COLOR: #007700">'url'<br></span>] = <span style="COLOR: #0000bb"></span>$data<span style="COLOR: #007700"></span><span style="COLOR: #dd0000"></span>; <span style="COLOR: #007700"> ケース </span><span style="COLOR: #0000bb">'著者'</span></font><font face="Courier New"><span style="COLOR: #007700">:<br> $temp</span><span style="COLOR: #0000bb">[</span><span style="COLOR: #007700">'著者'</span><span style="COLOR: #dd0000">] = </span></font> $data<font face="Courier New"><span style="COLOR: #007700"><br><br>;</span> <span style="COLOR: #dd0000"></span>$current_tag </font><font face="Courier New">= <span style="COLOR: #007700"><br>''</span><span style="COLOR: #0000bb"></span> <span style="COLOR: #007700">; </span> デフォルト: <span style="COLOR: #dd0000"> ブレーク </span> } <span style="COLOR: #007700">} </span><span style="COLOR: #0000bb"></span>?></font></span><span style="COLOR: #007700">関数 </span><span style="COLOR: #0000bb">return_page</span><font face="Courier New"><span style="COLOR: #007700">(){ <br> グローバル </span><span style="COLOR: #0000bb">$temp</span></font><font face="Courier New"> <span style="COLOR: #007700">; <br> echo </span><span style="COLOR: #dd0000">'o <A HREF="'</SPAN><SPAN style="COLOR: #007700">.</SPAN><SPAN style="COLOR: #0000bb">$temp</SPAN><SPAN style="COLOR: #007700">[</SPAN><SPAN style="COLOR: #dd0000">'url'</SPAN><SPAN style="COLOR: #007700">].</SPAN><SPAN style="COLOR: #dd0000">'">'</span><span style="COLOR: #007700">.</span><span style="COLOR: #0000bb">$temp</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'タイトル'</span><span style="COLOR: #007700">].</span><span style="COLOR: #dd0000">'</A><BR>'</span></font><font face="Courier New"><span style="COLOR: #007700">; <br>echo </span><span style="COLOR: #dd0000">'作成者: '</span><span style="COLOR: #007700">.</span><span style="COLOR: #0000bb">$temp</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'作成者'</span><span style="COLOR: #007700">].</span><span style="COLOR: #dd0000">'<br>'</span></font><font face="Courier New"><span style="COLOR: #007700">;<br>エコー </span><span style="COLOR: #dd0000">'------ - ---------'</span></font><font face="Courier New"><span style="COLOR: #007700">;<br>エコー </span><span style="COLOR: #dd0000">'<br>'</SPAN></FONT><SPAN style="COLOR: #007700"><FONT face="Courier New">;<br/>} <br/><br/></FONT></SPAN><FONT face="Courier New"><SPAN style="COLOR: #ff8000">// 解析内容 <br/></SPAN><SPAN style="COLOR: #0000bb">$xml_file </SPAN><SPAN style="COLOR: #007700">= </SPAN><SPAN style="COLOR: #dd0000">'slashdot.xml'</SPAN></FONT><SPAN style="COLOR: #007700"><FONT face="Courier New">; <br/><br/></FONT></SPAN><FONT face="Courier New"><SPAN style="COLOR: #ff8000">// 文字セットを定義します。デフォルトは UTF- 8 <br/></SPAN><SPAN style="COLOR: #0000bb">$type </SPAN><SPAN style="COLOR: #007700">= </SPAN><SPAN style="COLOR: #dd0000">'UTF-8'</SPAN></FONT><SPAN style="COLOR: #007700"><FONT face="Courier New"><br/><br/></FONT></SPAN>// パーサーを作成します <FONT face="Courier New"><SPAN style="COLOR: #ff8000"><br/>$xml_parser </SPAN><SPAN style="COLOR: #0000bb">= </SPAN><SPAN style="COLOR: #007700">xml_parser_create</SPAN><SPAN style="COLOR: #0000bb">(</SPAN> <SPAN style="COLOR: #007700">$type</SPAN><SPAN style="COLOR: #0000bb"></SPAN></FONT>); <SPAN style="COLOR: #007700"><FONT face="Courier New"><br/><br/></FONT></SPAN>// 解析オプションを設定します<FONT face="Courier New"><SPAN style="COLOR: #ff8000"><br/>xml_parser_set_option</SPAN><SPAN style="COLOR: #0000bb">(</SPAN><SPAN style="COLOR: #007700">$xml_parser</SPAN><SPAN style="COLOR: #0000bb">, </SPAN><SPAN style="COLOR: #007700">XML_OPTION_CASE_FOLDING</SPAN><SPAN style="COLOR: #0000bb">, </SPAN><SPAN style="COLOR: #007700">true</SPAN><SPAN style="COLOR: #0000bb"></SPAN></FONT>); 🎜><FONT face="Courier New">、<SPAN style="COLOR: #007700"><br/>'UTF-8'</SPAN><SPAN style="COLOR: #0000bb"></SPAN><SPAN style="COLOR: #007700">); 🎜></SPAN><SPAN style="COLOR: #0000bb">/ / 要素が見つかったときに呼び出す関数を PHP に指示します </SPAN>// これらの関数は要素の属性も処理します <SPAN style="COLOR: #007700"></SPAN><SPAN style="COLOR: #0000bb">xml_set_element_handler</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #dd0000">$xml_parser</SPAN></FONT>, <SPAN style="COLOR: #007700"><FONT face="Courier New">'startElement'<br/><br/>,</FONT></SPAN>'endElement'<FONT face="Courier New"><SPAN style="COLOR: #ff8000"><br/><br/>); </SPAN><SPAN style="COLOR: #0000bb"> </SPAN><SPAN style="COLOR: #007700"></SPAN><SPAN style="COLOR: #0000bb">//文字データに対して呼び出す関数を PHP に指示します</SPAN><SPAN style="COLOR: #007700"></SPAN>xml_set_character_data_handler<SPAN style="COLOR: #dd0000"></SPAN>(<SPAN style="COLOR: #007700"></SPAN>$xml_parser<SPAN style="COLOR: #dd0000"> </SPAN>, </FONT><SPAN style="COLOR: #007700">'characterData'<FONT face="Courier New"><br/><br/></FONT>); (!( </SPAN><FONT face="Courier New">$fp <SPAN style="COLOR: #ff8000"><br/>= </SPAN><SPAN style="COLOR: #0000bb">fopen</SPAN><SPAN style="COLOR: #007700">(</SPAN><SPAN style="COLOR: #0000bb">$xml_file</SPAN><SPAN style="COLOR: #007700">, </SPAN><SPAN style="COLOR: #dd0000"> 'r'</SPAN></FONT><FONT face="Courier New"><SPAN style="COLOR: #007700">))) { <br/>die(</SPAN><SPAN style="COLOR: #dd0000">"解析のために $xml_file ファイルを開けません!n"</SPAN></FONT><SPAN style="COLOR: #007700"><FONT face="Courier New">); <br/><br/><br/></FONT> </SPAN><FONT face="Courier New">// <SPAN style="COLOR: #ff8000"><br/></SPAN>while (<SPAN style="COLOR: #007700"></SPAN>$data <SPAN style="COLOR: #0000bb"></SPAN>= <SPAN style="COLOR: #007700"></SPAN>fread<SPAN style="COLOR: #0000bb">) をループしてファイル全体を解析します</SPAN>(<SPAN style="COLOR: #007700"></SPAN>$fp<SPAN style="COLOR: #0000bb"></SPAN>, <SPAN style="COLOR: #007700"></SPAN>4096<SPAN style="COLOR: #0000bb"></SPAN></FONT><FONT face="Courier New">)) { <SPAN style="COLOR: #007700"> if (!(<br/>) > </SPAN>$data <SPAN style="COLOR: #0000bb"></SPAN>= <SPAN style="COLOR: #007700"></SPAN>utf8_encode<SPAN style="COLOR: #0000bb"></SPAN>(<SPAN style="COLOR: #007700"></SPAN>$data<SPAN style="COLOR: #0000bb"></SPAN></FONT><FONT face="Courier New">))) { <SPAN style="COLOR: #007700"> echo <br/></SPAN>'ERROR'<SPAN style="COLOR: #dd0000"></SPAN>.<SPAN style="COLOR: #007700"></SPAN>"n"<SPAN style="COLOR: #dd0000"></SPAN></FONT><FONT face="Courier New"> } <SPAN style="COLOR: #007700">; if (!<br/><br/>xml_parse</SPAN><SPAN style="COLOR: #0000bb">(</SPAN><SPAN style="COLOR: #007700">$xml_parser</SPAN><SPAN style="COLOR: #0000bb">, </SPAN><SPAN style="COLOR: #007700">$data</SPAN><SPAN style="COLOR: #0000bb">, </SPAN> <SPAN style="COLOR: #007700">feof</SPAN><SPAN style="COLOR: #0000bb">(</SPAN><SPAN style="COLOR: #007700">$fp</SPAN><SPAN style="COLOR: #0000bb"></SPAN></FONT>))) { <FONT face="Courier New"> die(<SPAN style="COLOR: #007700"><br/>sprintf</SPAN> <SPAN style="COLOR: #0000bb">( </SPAN><SPAN style="COLOR: #007700">"XML エラー: %dnn 行目 %s"</SPAN><SPAN style="COLOR: #dd0000"></SPAN></FONT>, <FONT face="Courier New"> <SPAN style="COLOR: #007700"><br/>xml_error_string</SPAN><SPAN style="COLOR: #0000bb"> ( </SPAN><SPAN style="COLOR: #007700">xml_get_error_code</SPAN><SPAN style="COLOR: #0000bb">(</SPAN><SPAN style="COLOR: #007700">$xml_parser</SPAN><SPAN style="COLOR: #0000bb"></SPAN></FONT>))、<FONT face="Courier New"> <SPAN style="COLOR: #007700"><br/>xml_get_current_line_number</SPAN> <SPAN style="COLOR: #0000bb">(</SPAN><SPAN style="COLOR: #007700">$xml_parser</SPAN><SPAN style="COLOR: #0000bb"></SPAN></FONT>))); <FONT face="Courier New"> } <SPAN style="COLOR: #007700">} <br/><br/><br/><br/>xml_parser_free </SPAN><SPAN style="COLOR: #0000bb">(</SPAN><SPAN style="COLOR: #007700">$xml_parser</SPAN><SPAN style="COLOR: #0000bb"></SPAN></FONT>); <SPAN style="COLOR: #007700"><FONT face="Courier New"><br/><br/></FONT></SPAN>?> <span style="COLOR: #0000bb"><font face="Courier New"> </font> </span> <p style="width:100%;text-align:center;margin:10px 0"> <br> <br></p> <p style="width:100%;text-align:center;margin:10px 0"></p></span></font>