前面的mysql.php 是封装的mysql数据库功能,单例模式,所以取对象是静态方法 mysql::getObject();
代码很简单,如下所示:
- include 'mysql.php';
- $mysql= mysql::getObject();
- $mysql->query("SELECT * FROM post");
- $xml = new XMLWriter();
- $xml->openUri('hello.xml');
-
- $xml->setIndentString(' ');
- $xml->setIndent(true);
-
- $xml->startDocument('1.0', 'UTF-8');
-
-
- $xml->startElement('rss');
- $xml->writeAttribute('version','2.0');
-
-
- $xml->startElement('channel');
-
- $xml->startElement('title');
- $xml->text('title');
- $xml->endElement();
-
- $xml->startElement('link');
- $xml->text('http://foodstory.me/post/');
- $xml->endElement();
-
- $xml->startElement('description');
- $xml->text('');
- $xml->endElement();
-
- $xml->startElement('language');
- $xml->text('zh-cn');
- $xml->endElement();
-
- $xml->startElement('category');
- $xml->text('IT');
- $xml->endElement();
-
- $xml->startElement('copyright');
- $xml->text('copyright 2011 foodstory.me');
- $xml->endElement();
-
-
- while( $row = $mysql->fetch() )
- {
- $xml->startElement('item');
-
- $xml->startElement('title');
- $xml->text( $row['title']);
- $xml->endElement();
-
- $xml->startElement('link');
- $xml->text( 'http://foodstory.me/post/'.$row['id'].'.html');
- $xml->endElement();
-
- $xml->startElement('description');
- $xml->text( $row['text'] );
- $xml->endElement();
-
- $xml->startElement('pubDate');
- $xml->text( date('D, d M Y H:i:s T', $row['time']) );
- $xml->endElement();
-
-
- $xml->endElement();
- }
- $xml->endElement();
- $xml->endElement();
-
- $xml->endDocument();
-
openUri('') 方法的参数可以是一个文件,那么xml数据就写入到这个文件或者 php://output 输出到缓冲区,然后 flush方法输出到页面. |