Simple sample code for PHP XmlWriter Class (RSS output) _php tutorial

Source: Internet
Author: User
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 ');
//
$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 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 previous mysql.php is the encapsulated MySQL database feature, singleton mode, so the object is static method Mysql::getobject ();
The code is simple.
The argument to the OpenUri (") method can be a file, then the XML data is written to this file
or php://output output to a buffer, then flush method output to page

http://www.bkjia.com/PHPjc/324399.html www.bkjia.com true http://www.bkjia.com/PHPjc/324399.html techarticle Copy the code as follows: Include ' mysql.php '; $mysql = Mysql::getobject (); $mysql-query ("select * from post"); $xml = new XMLWriter () ; $xml-openuri (' hello.xml '); or ' PHP://OUTP ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.