Mysql
 sql >> Database >  >> RDS >> Mysql

parserror XML:posta indesiderata dopo l'elemento del 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();
  • Modificato in MySQL a MySQLi ! È importante iniziare a cambiare ora finché puoi! =)
  • Usa le funzioni! Scrivi $xml->startElement &$xml->endElement ripetutamente, inseriscilo in un addNode() metodo.
  • Aggiunta una 'Root ' per aggiungere anche ogni riga del database.
  • Usa CData (<![CDATA[text for inclusion within CData tags]]> ), ti aiuterà a sfuggire in sicurezza a qualsiasi personaggio difficile possibile