/**
* 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();
- Diubah menjadi MySQL ke MySQLi ! Sangat penting untuk mulai berubah sekarang selagi bisa! =)
- Gunakan Fungsi! Anda menulis
$xml->startElement
&$xml->endElement
berulang kali, letakkan di addNode()
metode.
- Menambahkan 'Root ' node untuk menambahkan setiap baris database juga.
- Gunakan
CData
(<![CDATA[text for inclusion within CData tags]]>
), ini akan membantu untuk melarikan diri dengan aman dari kemungkinan karakter yang sulit