<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">'<story>'</story></span></font><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>'</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>'</url></span></font><font face="Courier New"><span style="COLOR: #007700">,<br></span><span style="COLOR: #dd0000">'AUTHOR'</span><span style="COLOR: #007700">=> </span></font><span style="COLOR: #dd0000"><font face="Courier New">'<author>'<br></author></font></span><font face="Courier New"><span style="COLOR: #007700">); <br></span><span style="COLOR: #0000bb">$close_tags </span></font><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">''</span></font><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">''</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">''</span></font><font face="Courier New"><span style="COLOR: #007700">,<br></span><span style="COLOR: #dd0000">'AUTHOR'</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">function </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> global </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> break; <br> default: <br> break; <br> } <br> } <br>} <br><br></font></span><font face="Courier New"><span style="COLOR: #ff8000">// $current_tag告诉我们正在处理的标记,我们随后会在characterData函数中使用 <br>// <br>// 当遇到标记时我们知道要flush所有的临时变量准备操作下一个标记 <br></span><lt>'</lt></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">''</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">''</span></font><font face="Courier New"><span style="COLOR: #007700">,<br></span><span style="COLOR: #dd0000">'AUTHOR'</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">function </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> global </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> break; <br> default: <br> break; <br> } <br> } <br>} <br><br></font></span><font face="Courier New"><span style="COLOR: #ff8000">// $current_tag告诉我们正在处理的标记,我们随后会在characterData函数中使用 <br>// <br>// 当遇到标记时我们知道要flush所有的临时变量准备操作下一个标记 <br></span><span style="COLOR: #007700">function </span><span style="COLOR: #0000bb">endElement</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> global </span><span style="COLOR: #0000bb">$close_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> 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><span style="COLOR: #0000bb">$temp </span><span style="COLOR: #007700">= </span><span style="COLOR: #dd0000">''</span></font><span style="COLOR: #007700"><font face="Courier New">; <br> break; <br> default: <br> break; <br> } <br> } <br>} <br><br></font></span><font face="Courier New"><span style="COLOR: #ff8000">// 传送给此函数的是元素间的数据 <br>// 例如,对<title>Title Here</title>,$data就等于'Title Here' <br></span><span style="COLOR: #007700">function </span><span style="COLOR: #0000bb">characterData</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$parser</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">$data</span></font><font face="Courier New"><span style="COLOR: #007700">){ <br> global </span><span style="COLOR: #0000bb">$current_tag</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">$temp</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">$catID</span></font><font face="Courier New"><span style="COLOR: #007700">; <br> switch(</span><span style="COLOR: #0000bb">$current_tag</span></font><font face="Courier New"><span style="COLOR: #007700">){ <br> case </span><span style="COLOR: #dd0000">'TITLE'</span></font><font face="Courier New"><span style="COLOR: #007700">: <br> </span><span style="COLOR: #0000bb">$temp</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'title'</span><span style="COLOR: #007700">] = </span><span style="COLOR: #0000bb">$data</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: #dd0000">''</span></font><font face="Courier New"><span style="COLOR: #007700">; <br> break; <br> case </span><span style="COLOR: #dd0000">'URL'</span></font><font face="Courier New"><span style="COLOR: #007700">: <br> </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: #0000bb">$data</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: #dd0000">''</span></font><font face="Courier New"><span style="COLOR: #007700">; <br> break; <br>case </span><span style="COLOR: #dd0000">'AUTHOR'</span></font><font face="Courier New"><span style="COLOR: #007700">:<br> </span><span style="COLOR: #0000bb">$temp</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'author'</span><span style="COLOR: #007700">] = </span><span style="COLOR: #0000bb">$data</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: #dd0000">''</span></font><font face="Courier New"><span style="COLOR: #007700">; <br> default: <br> break; <br> } <br>} <br></span><span style="COLOR: #0000bb">?></span> <br><br><br></font><font face="Courier New"><span style="COLOR: #0000bb"><?php <br/><br></span><span style="COLOR: #007700">function </span><span style="COLOR: #0000bb">return_page</span></font><font face="Courier New"><span style="COLOR: #007700">(){ <br> global </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%20style=" color:>.</a></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">'title'</span><span style="COLOR: #007700">].</span><span style="COLOR: #dd0000">'<br>'</span></font><font face="Courier New"><span style="COLOR: #007700">; <br>echo </span><span style="COLOR: #dd0000">'Author:'</span><span style="COLOR: #007700">.</span><span style="COLOR: #0000bb">$temp</span><span style="COLOR: #007700">[</span><span style="COLOR: #dd0000">'author'</span><span style="COLOR: #007700">].</span><span style="COLOR: #dd0000">'<br>'</span></font><font face="Courier New"><span style="COLOR: #007700">;<br>echo </span><span style="COLOR: #dd0000">'-----------------------------'</span></font><font face="Courier New"><span style="COLOR: #007700">;<br>echo </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></span><span style="COLOR: #0000bb">$xml_parser </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">xml_parser_create</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$type</span></font><span style="COLOR: #007700"><font face="Courier New">); <br><br></font></span><font face="Courier New"><span style="COLOR: #ff8000">// 设置解析选项 <br></span><span style="COLOR: #0000bb">xml_parser_set_option</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$xml_parser</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">XML_OPTION_CASE_FOLDING</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">true</span></font><font face="Courier New"><span style="COLOR: #007700">); <br></span><span style="COLOR: #0000bb">xml_parser_set_option</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$xml_parser</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">XML_OPTION_TARGET_ENCODING</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">// 告诉PHP发现元素时要调用什么函数 <br>// 这些函数同时也处理元素的属性 <br></span><span style="COLOR: #0000bb">xml_set_element_handler</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$xml_parser</span><span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">'startElement'</span><span style="COLOR: #007700">,</span><span style="COLOR: #dd0000">'endElement'</span></font><span style="COLOR: #007700"><font face="Courier New">); <br><br></font></span><font face="Courier New"><span style="COLOR: #ff8000">//告诉PHP对字符数据调用什么函数 <br></span><span style="COLOR: #0000bb">xml_set_character_data_handler</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$xml_parser</span><span style="COLOR: #007700">, </span><span style="COLOR: #dd0000">'characterData'</span></font><font face="Courier New"><span style="COLOR: #007700">); <br><br>if (!(</span><span style="COLOR: #0000bb">$fp </span><span style="COLOR: #007700">= </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><span style="COLOR: #007700">while (</span><span style="COLOR: #0000bb">$data </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">fread</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$fp</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">4096</span></font><font face="Courier New"><span style="COLOR: #007700">)) { <br> if (!(</span><span style="COLOR: #0000bb">$data </span><span style="COLOR: #007700">= </span><span style="COLOR: #0000bb">utf8_encode</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$data</span></font><font face="Courier New"><span style="COLOR: #007700">))) { <br> echo </span><span style="COLOR: #dd0000">'ERROR'</span><span style="COLOR: #007700">.</span><span style="COLOR: #dd0000">"n"</span></font><font face="Courier New"><span style="COLOR: #007700">; <br> } <br> if (!</span><span style="COLOR: #0000bb">xml_parse</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$xml_parser</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">$data</span><span style="COLOR: #007700">, </span><span style="COLOR: #0000bb">feof</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$fp</span></font><font face="Courier New"><span style="COLOR: #007700">))) { <br> die(</span><span style="COLOR: #0000bb">sprintf</span><span style="COLOR: #007700">( </span><span style="COLOR: #dd0000">"XML error: %s at line %dnn"</span></font><font face="Courier New"><span style="COLOR: #007700">, <br> </span><span style="COLOR: #0000bb">xml_error_string</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">xml_get_error_code</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$xml_parser</span></font><font face="Courier New"><span style="COLOR: #007700">)), <br> </span><span style="COLOR: #0000bb">xml_get_current_line_number</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$xml_parser</span></font><font face="Courier New"><span style="COLOR: #007700">))); <br> } <br>} <br><br></span><span style="COLOR: #0000bb">xml_parser_free</span><span style="COLOR: #007700">(</span><span style="COLOR: #0000bb">$xml_parser</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> <p class="clear"></p>