Problems with special characters in an XML file created with PHP

0

I have a php script that generates an xml of some tables and it seemed to work correctly, but now one of the values of the fields has a "Ñ" and the browsers take it as an error:

XML read error: malformed

Line number 33, column 36: ARTURO CHAVEZ NUÑ

part of my code is this:

<?php


function crea_fichero($cadena){

    $flujo = fopen('club.xml', 'w');
    fputs($flujo, $cadena);
    fclose($flujo);
}


$opc = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
$dsn = "mysql:host=localhost;dbname=prueba_importar";
$usuario = 'root';
$contrasena = '';

$dwes = new PDO($dsn, $usuario, $contrasena, $opc);


if (isset($dwes)){

   $xml="<xml>\n";
   $xml.="\t<club>".date("d/m/Y")."</club>\n";

          $sql2="SELECT * FROM alumnos INNER JOIN (alumnosexamen INNER JOIN examen ON examen.claveexamen=alumnosexamen.claveexamen) ON alumnosexamen.claveweb= alumnos.claveweb AND alumnosexamen.clavealumno=alumnos.clavealumno WHERE examen.estatus='APROBADO' AND alumnos.clavealumno=0 ";


        echo '<br>';

        $cadena="alumnos";

        $xml .= "\t<".$cadena.">\n";
        $result= $dwes->query($sql2);

        $tamanio=strlen($cadena);
        $cadena2=substr($cadena,0,($tamanio-1));

        while($fila = $result->fetch(PDO::FETCH_ASSOC)){
            print_r($fila);

            $xml .= "\t\t<".$cadena2.">\n";

                        $clavealumno=utf8_decode($fila['clavealumno']);
                        $claveweb=utf8_decode($fila['claveweb']);
                        $claveescuela=utf8_decode($fila['claveescuela']);
                        $nombre=utf8_decode( $fila['nombre']);

                        $domicilio=utf8_decode($fila['domicilio']);
                        $ciudad=utf8_decode($fila['ciudad']);
                        $estado=utf8_decode($fila['estado']);
                        $telefono=utf8_decode( $fila['telefono']);

            $xml .= "\t\t</".$cadena2.">\n";
        }// while($fila
        $xml .= "\t</".$cadena.">\n";

    $xml .="</xml>";
crea_fichero($xml);
}//if(isset($dwes))
$result=null;
$dwes=null;

?>
    
asked by Condor_G 20.04.2018 в 04:40
source

1 answer

0

In the code that you sample you can see that the data you extract is treated as UTF8, now check that your code to build the XML also takes into account the use of UTF8 either through "" at the start of your file or by indicating the UTF8 encoding to the object that you are using to build the XML. Reference 1: link

    
answered by 20.04.2018 в 17:33