0

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 .= "\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;
        }

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

Wolfchamane
  • 255
  • 1
  • 5
  • 23
  • 1
    Check your encodings, the HTTP header you set has no encodung, thus the client expects latin1 or such, the XML document then claims UTF-8 ... But which encoding are the ñ in? – johannes Jun 24 '14 at 10:36
  • Done, changed to `header("Content-Type", "text/xml;charset=UTF-8")`, same error – Wolfchamane Jun 24 '14 at 10:48

2 Answers2

0

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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
0

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Shahzad Farukh
  • 317
  • 1
  • 2
  • 17