search
Homephp教程php手册php XMLWriter类的简单示例代码(RSS输出)

php XMLWriter类的简单示例代码(RSS输出)

Jun 13, 2016 pm 12:04 PM
includephprsscodecopyofExampleSimplekindoutput

复制代码 代码如下:


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');
//
$xml->startElement('rss');
$xml->writeAttribute('version','2.0');
//
$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/'.$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();


前面的mysql.php 是封装的mysql数据库功能,单例模式,所以取对象是静态方法 mysql::getObject();
代码很简单
openUri('') 方法的参数可以是一个文件,那么xml数据就写入到这个文件
或者 php://output 输出到缓冲区,然后 flush方法输出到页面
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),