2014-06-24 135 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