Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Parserror XML:lixo após elemento de documento

    /**
     *  Rewrite MySQL to MySQLi
     *  http://php.net/manual/en/migration55.deprecated.php
    **/
    $sql = "SELECT * FROM projecten ORDER BY id";
    $res = mysqli_query($sql);

    /**
     *  Adjusted startDoc to include
     *  Encoding and set Indent to tab
     *  instead of spacing.
    **/
    $xml = new XMLWriter();
    $xml->openURI("php://output");
    $Xml->setIndentString ( "	" );
    $Xml->startDocument( '1.0', 'UTF-8', 'no' );
    $xml->setIndent(true);

    /**
     *  Re-usable function to
     *  write a Start/End Element 
     *  to XMLWriter.
     *
     *  Uses CDdata for longer text strings
    **/
    function addNode( $xml, $NodeName, $Value ) {
        $xml->startElement( $NodeName );
        $xml->writeCData( $Value );
        $xml->endElement();      
    }

    /**
     *  Start a Root Element
    **/
   $xml->startElement('projects');

    /**
     *  LoopThrough each Database row.
    **/
   while ($row = mysqli_fetch_assoc($res)) {

        //START PROJECT
        $xml->startElement('project');
        $xml->writeAttribute('id', $row['id']);

            addNode( $xml, "Id", $row["id"] );
            addNode( $xml, "projectnaam", $row['projectnaam'] );
            addNode( $xml, "status", $row['status'] );
            addNode( $xml, "klantnaam", $row['klantnaam'] );
            addNode( $xml, "startdatum", $row['startdatum'] );
            addNode( $xml, "eindddatum", $row['eindddatum'] );

        //End 'Project'.
        $xml->endElement();
    }

    //End Root Element.
    $xml->endElement();

    //EINDE DOCUMENT
    $xml->endDocument(); 

    header('Content-type: text/xml');
    $xml->flush();
  • Alterado para MySQL para MySQLi ! É importante começar a mudar agora enquanto você pode! =)
  • Use funções! Você escreve $xml->startElement &$xml->endElement repetidamente, colocou-o em um addNode() método.
  • Adicionou uma 'Raiz ' para anexar cada linha do banco de dados também.
  • Use CData (<![CDATA[text for inclusion within CData tags]]> ), ajudará a escapar com segurança de quaisquer caracteres difíceis possíveis