0

I'm using this code to create an XML file (not filed) from a PHP one. Here is the code:

<?php
include_once ('conf.php');
$conn =  mysql_connect($host, $user, $password);    
if (!$conn) {
    die('No hay conexion a la BBDD');
}
$bd = mysql_select_db($name, $conn);
if (!$bd) {
    die ('Error en la BBDD');
}
$query = "select * from usuarios where activo = 0 order by puntuacion desc limit 0, 10";
$res = mysql_query($query, $conn);
$salida = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>'."\n";
$salida .= '<score>'."\n";
$i = 1;
while ($row = mysql_fetch_array($res))
{
    $salida.= '<posicion num="' . $i . '">'."\n";
    $salida .= '<id>'.$row['id'].'</id>'."\n";
    $salida .= '<puntuacion>'.$row['puntuacion'].'</puntuacion>'."\n";
    $salida .= '</posicion>'."\n";
    $i++;
}
$salida .= '</score>';
mysql_free_result($res);
mysql_close($conn);
echo $salida;
?>

When I call this file I obtain (using Chrome Inspector) the XML file embeded in a HTML file with its html, head and body tags. I want this php file to get readed by an ajax's get function.

Any ideas about what is wrong?

2

2 Answers 2

2

Add a header specifying that you're outputting XML. header('Content-Type: text/xml') right before you echo.

Sign up to request clarification or add additional context in comments.

1 Comment

Fixed!!! Thanks all of you.
2

Read the PHP manual about XML Manipulation.

You will find juicy tools there to fetch, manipulate and create XML documents.

Also, before making any output, add a line header ("Content-type: text/xml"); to your script, to specify for the client entity, that you are going to send XML and it should be parsed as XML. header — Send a raw HTTP header

2 Comments

That's it. It's very difficult to generate valid XML with raw echo statements.
@ÁlvaroG.Vicario: Actually it's equally difficult to generate valid HTML with raw ech statements, that's why you use DOMDocument for the HTML all the time, right? ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.