Simple example of the phpXMLWriter class

Source: Internet
Author: User
The preceding mysql. php is a encapsulated mysql database function in Singleton mode. Therefore, the static method mysql: getObject () is used to retrieve the object. the code is very simple, as shown below :? Mysql. php in front of phpinclude... is the encapsulated mysql database function in Singleton mode. Therefore, the object is the static method mysql: getObject ();

The code is as follows:

 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://foodstory.me/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 foodstory.me');$xml->endElement();// for itemwhile ($row = $mysql->fetch()) {    $xml->startElement('item');    // title    $xml->startElement('title');    $xml->text($row['title']);    $xml->endElement();    // link    $xml->startElement('link');    $xml->text('http://foodstory.me/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();?>
  
 

The parameter of the openUri ('') method can be a file, so xml data is written to this file or php: // output is output to the buffer, and then flush is output to the page.


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.