打印XML数组时出现问题

时间:2014-09-01 22:40:01

标签: php xml

我有以下代码,它将所有XML字段中的“money”取出,但只有1条记录,并向我显示此错误:

错误:

 Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in /var/www/client/lib/class/index.php on line 34

Warning: SimpleXMLElement::__construct(): %PDF-1.4 in /var/www/client/lib/class/index.php on line 34

Warning: SimpleXMLElement::__construct(): ^ in /var/www/client/lib/class/index.php on line 34

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /var/www/client/lib/class/index.php:34 Stack trace: #0 /var/www/client/lib/class/index.php(34): SimpleXMLElement->__construct('%PDF-1.4?%?????...') #1 /var/www/client/lib/class/index.php(47): uuid->select() #2 {main} thrown in /var/www/client/lib/class/index.php on line 34

我的代码:

$data = $this -> conect -> conexion();          
           $dbquery = $data -> prepare("SELECT * 
                                        FROM FILE 
                                        ORDER BY ID
                                      ");


                 $dbquery->execute();
       while($rows = $dbquery->fetch(PDO::FETCH_ASSOC)){
              $string = $rows['BYTES'];
                $tuxml = new SimpleXMLElement($string);
         echo $tuxml->attributes()->Moneda;

1 个答案:

答案 0 :(得分:2)

分析

正确阅读异常信息可以揭示问题的根源:

Entity: line 1: parser error : Start tag expected, '<' not found

结论:您尝试解析无效XML的内容

SimpleXMLElement->__construct('%PDF-1.4?%?????...')

结论:您似乎正在尝试解析PDF文件,如幻数&#34;%PDF&#34;:

  

幻数:
        所有PDF文件都以字符&#39;%PDF开头 - &#39;使用PDF
        版本号,例如,&#39;%PDF-1.4&#39;。这些字符在美国         ASCII编码。

     

(来源:RFC 3778,第9页,http://tools.ietf.org/html/rfc3778


分辨率

  • 确保您的源仅包含有效的XML文件
  • 尝试检测文件的mime类型并跳过非XML文件
  • 添加一个周围的try-catch块来处理不能解析为XML的文件