Simple sample code of the php XMLWriter class (RSS output)

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 ');
// <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

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.