Copy Code code as follows:
Include ' mysql.php ';
$mysql = Mysql::getobject ();
$mysql->query ("select * from post");
$xml = new XMLWriter ();
$xml->openuri (' hello.xml '); or ' Php://output '
$xml->setindentstring (");
$xml->setindent (TRUE);
Start
$xml->startdocument (' 1.0 ', ' UTF-8 ');
<rss version= "2.0" >
$xml->startelement (' RSS ');
$xml->writeattribute (' Version ', ' 2.0 ');
<channel>
$xml->startelement (' channel ');
Title
$xml->startelement (' title ');
$xml->text (' title ');
$xml->endelement ();
Link
$xml->startelement (' link ');
$xml->text (' http://jb51.net/post/');
$xml->endelement ();
Description
$xml->startelement (' description ');
$xml->text (");
$xml->endelement ();
Language
$xml->startelement (' language ');
$xml->text (' ZH-CN ');
$xml->endelement ();
Category
$xml->startelement (' category ');
$xml->text (' IT ');
$xml->endelement ();
Copyright
$xml->startelement (' copyright ');
$xml->text (' copyright jb51.net ');
$xml->endelement ();
For item
while ($row = $mysql->fetch ())
{
$xml->startelement (' item ');
Title
$xml->startelement (' title ');
$xml->text ($row [' title ']);
$xml->endelement ();
Link
$xml->startelement (' link ');
$xml->text (' http://jb51.net/post/'. $row [' id ']. HTML ');
$xml->endelement ();
Description
$xml->startelement (' description ');
$xml->text ($row [' text ']);
$xml->endelement ();
pubdate
$xml->startelement (' pubdate ');
$xml->text (Date (' d, D M Y h:i:s T ', $row [' time ']));
$xml->endelement ();
Category tag author need to write.
$xml->endelement (); Item
}
$xml->endelement (); Channel
$xml->endelement (); Rss
$xml->enddocument ();
$xml->flush ();
The front of the mysql.php is encapsulated MySQL database function, single case mode, so take object is static method Mysql::getobject ();
The code is simple.
The parameter of the Openuri (") method can be a file, then the XML data is written to this file
or php://output output to the buffer, and then flush the method to the page