Inside a DDBB a have the following data:
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"
Got the following function script:
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 .= "
<categories>";
foreach($categories as $category){
$ret1 .= "
\t<category>";
$ret1 .= "
\t\t<addedat>".$category->getCatDate()."</addedat>";
$ret1 .= "
\t\t<name>".$category->getCatName()."</name>";
$ret1 .= "
\t\t<key>".$category->getCatKey()."</key>";
$ret1 .= "
\t</category>";
}//foreach
$ret1 .= "
</categories>";
$ret = trim($ret1);
}else{
$ret = new Error(self::$errorFilePath, "ERROR: no se pudo listar las categorias. MySQL = ".self::$ddbb->getError());
}
return $ret;
}
After this function, a super 'controller.php' do the following:
header("Content-Type", "text/xml");
header_response_code(200);
echo $ret;
But the script returns the following XML file:
<?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>
And jQuery claims Invalid XML
You should use a library that can encode strings into XML properly like SimpleXML instead of doing string concatenations:
$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');
The only precondition for this to work is that teh getters of $category
(that are the methods like $category->getCatDate()
) are returning UTF-8 encoded strings.
If they don't you'll see errors - but you'll see them early. See as well:
and ensure you've got error logging enabled so that you can track the errors when you're doing AJAX interaction.
I think the problem is in the following code:
$aux->setCatDate($row[0]);
$aux->setCatName($row[1]);
$aux->setCatKey($row[2]);
Try to use the column name to get the $row data from DB, like:
$aux->setCatDate($row['addedat']);
$aux->setCatName($row['catname']);
$aux->setCatKey($row['catkey']);
and then see the result.