【问题标题】:PHP returns invalid XMLPHP 返回无效的 XML
【发布时间】:2014-06-24 10:33:30
【问题描述】:

在 DDBB 内部有以下数据:

SELECT `addedat`, `catname`, `catkey` FROM `categorias`;
"2014-06-23"    "Complementos"  "complementos"
"2014-06-23"    "Hombre"    "hombre"
"2014-06-23"    "Mujer" "mujer"
"2014-06-23"    "Niños y bebes" "niños_y_bebes"

得到以下函数脚本:

public function listAllCategories(){
            $ret = null;
            $result = self::$ddbb->executeQuery(self::$dao->getQueryGetAllCategories());
            if ($result && (mysql_num_rows($result) !== 0)){

                $categories = array();
                while($row = mysql_fetch_row($result)){
                    $aux = new Categoria();
                    $aux->setCatDate($row[0]);
                    $aux->setCatName($row[1]);
                    $aux->setCatKey($row[2]);
                    array_push($categories, $aux);
                }//while

                mysql_free_result($result);

                $ret1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
                $ret1 .= "\n<categories>";
                foreach($categories as $category){
                    $ret1 .= "\n\t<category>";
                    $ret1 .= "\n\t\t<addedat>".$category->getCatDate()."</addedat>";
                    $ret1 .= "\n\t\t<name>".$category->getCatName()."</name>";
                    $ret1 .= "\n\t\t<key>".$category->getCatKey()."</key>";
                    $ret1 .= "\n\t</category>";
                }//foreach
                $ret1 .= "\n</categories>";

                $ret = trim($ret1);

            }else{
                $ret = new Error(self::$errorFilePath, "ERROR: no se pudo listar las categorias. MySQL = ".self::$ddbb->getError());
            }
            return $ret;
        }

在这个函数之后,一个超级 'controller.php' 做以下事情:

header("Content-Type", "text/xml");
header_response_code(200);
echo $ret;

但脚本返回以下 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<categories>
    <category>
        <addedat>2014-06-23</addedat>
        <name>Niños y bebes</name>
        <key>niños_y_bebes</key>
    </category>
    <category>
        <addedat>2014-06-23</addedat>
        <name>Niños y bebes</name>
        <key>niños_y_bebes</key>
    </category>
    <category>
        <addedat>2014-06-23</addedat>
        <name>Niños y bebes</name>
        <key>niños_y_bebes</key>
    </category>
    <category>
        <addedat>2014-06-23</addedat>
        <name>Niños y bebes</name>
        <key>niños_y_bebes</key>
    </category>
</categories>

而 jQuery 声称 无效的 XML

【问题讨论】:

  • 检查您的编码,您设置的 HTTP 标头没有编码,因此客户端需要 latin1 或类似的,XML 文档然后声明 UTF-8 ...但是 ñ 是哪种编码?跨度>
  • 完成,改成header("Content-Type", "text/xml;charset=UTF-8"),同样的错误

标签: php jquery xml


【解决方案1】:

您应该使用可以将字符串正确编码为 XML 的库,例如 SimpleXML,而不是进行字符串连接:

$ret = new SimpleXMLElement('<categories/>');

foreach ($categories as $category) {
    $category          = $ret->addChild('category');
    $category->addedat = $category->getCatDate();
    $category->name    = $category->getCatName();
    $category->key     = $category->getCatKey();
}

$ret->asXML('php://output');

唯一的前提条件是 $category 的 getter(即 $category-&gt;getCatDate() 之类的方法)返回 UTF-8 编码的字符串。

如果没有,您会看到错误 - 但您会很早就看到它们。另请参阅:

并确保您已启用错误日志记录,以便在进行 AJAX 交互时跟踪错误。

【讨论】:

    【解决方案2】:

    我认为问题出在以下代码中:

    $aux->setCatDate($row[0]);
    $aux->setCatName($row[1]);
    $aux->setCatKey($row[2]);
    

    尝试使用列名从DB中获取$row数据,如:

    $aux->setCatDate($row['addedat']);
    $aux->setCatName($row['catname']);
    $aux->setCatKey($row['catkey']);
    

    然后看看结果。

    【讨论】:

    • 随着这些变化,它们返回空值
    猜你喜欢
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2018-10-29
    • 2013-09-22
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    相关资源
    最近更新 更多