我用xml_parser解析xml,但是处理的xml中有的含有乱码,于是报错,自己研究了好几天,没有办法,请高手帮忙解决一下,谢谢!!我的代码就是网上找的:
     1 <?php
      2 $open_tags = array(
      3     'Q' => '<Q>',
      4     'M' => '<M>',
      5     'STORY' => '<STORY>',
      6     'TITLE' => '<TITLE>',
      7     'URL' => '<URL>',
      8     'AUTHOR'=> '<AUTHOR>'
      9 );
     10 $close_tags = array(
     11     'Q' => '</Q>',
     12     'M' => '</M>',
     13     'STORY' => '</STORY>',
     14     'TITLE' => '</TITLE>',
     15     'URL' => '</URL>',
     16     'AUTHOR'=> '</AUTHOR>'
     17 );
     18 
     19 
     20 
     21 
     22 //面就是定义函数来提取数据:
     23 
     24 // 处理开始标记的属性指
     25 // $attrs是一个多维数组,键值为属性名, 值就是该属性的值
     26 function startElement($parser, $name, $attrs=''){
     27 global $open_tags, $temp, $current_tag;
     28 $current_tag = $name;
     29 if ($format = $open_tags[$name]){
     30 switch($name){
     31 case 'Q':
     32     echo '有q ';
     33 case 'STORY':
     34 echo '新的故事: ';
     35 break;
     36 default:
     37 break;
     38 }
     39 }
     40 }
     41 
     42 // $current_tag告诉我们正在处理的标记,我们随后会在characterData函数中使用
     43 //
     44 // 当遇到</STORY>标记时我们知道要flush所有的临时变量准备操作下一个标记
     45 function endElement($parser, $name, $attrs=''){
     46 global $close_tags, $temp, $current_tag;
     47 if ($format = $close_tags[$name]){
     48 switch($name){
     49 case 'M':
     50 return_title($temp);
     51 break;
     52 case 'STORY':
     53 return_page($temp);
     54 $temp = '';
     55 break;
     56 default:
     57 break;
     58 }
     59 }
     60 }
     61 
     62 // 传送给此函数的是元素间的数据
     63 // 例如,对<TITLE>Title Here</TITLE>,$data就等于'Title Here'
     64 function characterData($parser, $data){
     65 global $current_tag, $temp, $catID;
     66 switch($current_tag){
     67 case 'Q':
     68 $temp['Q'] = $data;
     69 $current_tag = '';
     70 case 'M':
     71 $temp['M'] = $data;
     72 $current_tag = '';
     73 case 'TITLE':
     74 $temp['title'] = $data;
     75 $current_tag = '';
     76 break;
     77 case 'URL':
     78 $temp['url'] = $data;
     79 $current_tag = '';
     80 break;
     81 case 'AUTHOR':
     82 $temp['author'] = $data;
     83 $current_tag = '';
     84 default:
     85 break;
     86 }
     87 }
     88 
     89 
     90 
     91 
     92 
     93 function return_page(){
     94 global $temp;
     95 echo 'o <A HREF="'.$temp['url'].'">'.$temp['title'].'</A><BR>';
     96 echo 'Author:'.$temp['author'].'<BR>';
     97 echo '-----------------------------';
     98 echo '<br/>';
     99 }
    100 
    101 function return_title(){
    102     global $temp;
    103     echo '你要找的是'.$temp['Q'].'共'.$temp['M'].'个';
    104     echo '<br/>';
    105 }
    106 
    107 // 分析的内容
    108 $xml_file = 'slashdot.xml';
    109 
    110 // 定义字符集,默认是UTF-8
    111 $type = 'UTF-8';
    112 
    113 // 建立解析器
    114 $xml_parser = xml_parser_create($type);
    115 
    116 // 设置解析选项
    117 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
    118 xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
    119 
    120 // 告诉PHP发现元素时要调用什么函数
    121 // 这些函数同时也处理元素的属性
    122 xml_set_element_handler($xml_parser, 'startElement','endElement');
    123 
    124 //告诉PHP对字符数据调用什么函数
    125 xml_set_character_data_handler($xml_parser, 'characterData');
    126 
    127 if (!($fp = fopen($xml_file, 'r'))) {
    128 die("无法打开 $xml_file 文件进行解析!n");
    129 }
    130 
    131 // 通过循环来解析整个文件
    132 while ($data = fread($fp, 4096)) {
    133 
    134 
    135 if (!xml_parse($xml_parser, $data, feof($fp))) {
    136 die(sprintf( "XML error: %s at line %dnn",
    137 xml_error_string(xml_get_error_code($xml_parser)),
    138 xml_get_current_line_number($xml_parser)));
    139 }
    140 }
    141 
    142 xml_parser_free($xml_parser);
    143 ?>

解决方案 »

  1.   

    错误是:
    Warning: xml_parse() [function.xml-parse]: input conversion failed due to input error, bytes 0xF8 0xB1 0xED 0xCA in /usr/local/apache2/htdocs/testxml.php on line 135xml是:
         1 <?xml version="1.0" encoding="gbk" ?>
          2 <backslash xmlns:backslash="http://slashdot.org/backslash.dtd">
          3 <q>sdfsdf</q>
          4     <m>308000</m>
          5     <sn>1</sn>
          6     <en>20</en>
          7     <used_time>5</used_time>
          8     <relative_word />
          9     <story>
         11         <url>http://slashdot.org/article.pl?sid=07/08/25/0139210</url>
         12         <time>2007-08-25 03:29:00</time>
         13         <author>kdawson</author>
         14         <department>homeland-security-card</department>
         15         <topic>216</topic>
         16         <comments>0</comments>
         17         <section>askslashdot</section>
         18         <image>topicrobotics.gif</image>
         19     </story>
         20 
         21     <story>
         22         <title>专集名称:黑色柳丁 发行公司:AvexTrax 发行时间:2002年 语言种类:国语 『最新更新专集』 『将此专集收藏』 √复制...
            为什么碟名取为「黑色柳丁」陶 丛硎荆∈翘ㄍ宥烙械乃抢锸撬氖乱蹈运粤∮兄帜那橐饨...</title>
         23         <url>http://slashdot.org/article.pl?sid=07/08/25/019234</url>
         24         <time>2007-08-25 02:02:00</time>
         25         <author>kdawson</author>
         26         <department>finally-experimental-data</department>
         27         <topic>160</topic>
         28         <comments>19</comments>
         29         <section>science</section>
         30         <image>topicspace.gif</image>
         31     </story>
         32 
         33     <story>
         34         <title>BioShock Installs a Rootkit</title>
         35         <url>http://slashdot.org/article.pl?sid=07/08/24/213256</url>
         36         <time>2007-08-25 00:36:00</time>
         37         <author>kdawson</author>
         38         <department>drm-even-in-the-demo</department>
         39         <topic>172</topic>
         40         <comments>222</comments>
         41         <section>games</section>
         42         <image>topicsecurity.gif</image>
         43     </story>
         44 
         45     <story>
         46         <title>Sys Admin Magazine Ceases Publication</title>
         47         <url>http://slashdot.org/article.pl?sid=07/08/24/1945228</url>
         48         <time>2007-08-24 23:08:00</time>
         49         <author>kdawson</author>
         50         <department>paper-is-obsolete-anyway</department>
         51         <topic>130</topic>
         52         <comments>71</comments>
         53         <section>linux</section>
         54         <image>topicunix.gif</image>
         55     </story>
         56 
         57
         58 
         59 </backslash>
      

  2.   

    我手动把里面的乱码删掉就没问题了,能不能有办法,删除字符串中的乱码呢,就是,判断是不是UTF-8里面有的编码,没有就删掉?