【问题标题】:XML Parse Error: error on line 1 at column 769: Encoding errorXML 解析错误:第 1 行第 769 列的错误:编码错误
【发布时间】:2013-06-03 03:50:15
【问题描述】:

此问题是从此处开始的上一个问题的扩展。

XML Parse Error - Extra content at the end of the document

我添加了一个 sn-p 代码来列出姓名和电话号码..

if(isset(${'Action'}) && ${'Action Type'}){
                if(${'Action'} == 'get'){
                    if(${'Limit'} != ''){
                        $limit = ' LIMIT '.${'Limit'}.'';
                    } else {
                        $limit = '';
                    }
                    $i = 1;
                    ${'Response'}['numbers'] = array();
                    ${'Query'} = mysql_query('SELECT * FROM `crm`.`accounts` WHERE `acquired_via` = "Scrubbed account" AND `sent_to_dialer` = "0"'.$limit);
                    while(${'Row'} = mysql_fetch_assoc(${'Query'})){
                        ${'Response'}['numbers']['number_'.$i] = array('Company_Name' => ${'Row'}['company_name'], 'Contact_First_Name' => ${'Row'}['contact_fname'], 'Contact_Last_Name' => ${'Row'}['contact_lname'], 'Office_Phone' => removeCHARSphone(${'Row'}['office_phone']), 'Mobile_Phone' => removeCHARSphone(${'Row'}['mobile_phone']));
                        $i++;
                    }
                }elseif(${'Action'} == 'details'){

                }
            }

这会破坏 XML

http://lmsapi.com/?api_key=b3e04e54f0d92f8845d394b61c607d60&act=get&format=xml http://lmsapi.com/?api_key=b3e04e54f0d92f8845d394b61c607d60&act=get&format=json

但 JSON 保持完整...

【问题讨论】:

  • 将所有 & 替换为 & 与 >你应该通过
  • 向我们展示你的arrayToXML() 函数,问题就在那儿。

标签: php xml json


【解决方案1】:

您需要转义 XML 内容中的所有 & 和尖括号。

所以&应该变成&<应该变成<>应该变成>

最简单的方法可能是像这样使用str_replace 调用:

$string = str_replace(
  array('&', '<', '>'),
  array('&amp;', '&lt;', '&gt;'),
  $string);

【讨论】:

  • htmlentities() 不是比str_replace() 更容易吗?
  • @Barmar htmlentities 的问题在于,它还会替换任意数量的实体,而这些实体是您不想替换且在 xml 中不受支持的。
  • 很惊讶没有类似的xmlentities()函数。
  • @Barmar 我可能不得不收回 - 查看文档,似乎可以为 XML 设置标志。这对我来说是新的。看起来它是在 5.4.0 中添加的。所以要看你需要支持什么版本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 2017-02-07
  • 1970-01-01
  • 2021-03-25
  • 2019-08-21
  • 1970-01-01
  • 2021-11-08
相关资源
最近更新 更多