2014-06-24 137 views
0

里面一个DDBB一个有以下数据:PHP返回无效的XML

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

+1

检查你的编码,你设置的HTTP头没有编码,因此客户端期望latin1或类似的,XML文件然后声明UTF-8 ...但哪些编码是在? – johannes

+0

完成,更改为'header(“Content-Type”,“text/xml; charset = UTF-8”),同样的错误 – Wolfchamane

回答

0

您应该使用可以编码字符串转换为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(即像$category->getCatDate()这样的方法)的获得者正在返回UTF-8编码的字符串。

如果他们没有,你会看到错误 - 但你会提前看到他们。见还有:

,并确保您已启用了错误日志记录,这样就可以当你做AJAX交互跟踪误差。

0

我认为这个问题是在下面的代码:

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

尝试使用列名从DB获得$行数据,如:

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

,然后看看结果。

+0

通过这些更改,它们返回空值 – Wolfchamane