Copy codeThe Code is 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 2011 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/'.w.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. over
$ Xml-> endElement (); // item
}
$ Xml-> endElement (); // channel
$ Xml-> endElement (); // rss
$ Xml-> endDocument ();
// $ Xml-> flush ();
The preceding mysql. php is an encapsulated mysql database function in singleton mode. Therefore, the object is taken as a static mysql: getObject ();
The code is simple.
The parameter of the openUri ('') method can be a file, so xml data is written to this file.
Or php: // output to the buffer, and then flush to the page