Php5.0 code: Use a domdocument object to generate an RSS document

Source: Internet
Author: User
Tags cdata

<〈? PHP
/*
Use the domdocument object to generate an RSS document.
Applicable to PhP 5.0 and later versions
*/

$ Dom = new domdocument ('1. 0 ');
// $ Dom-> xmlencoding = "gb2312";

// Add an RSS Element
$ RSS = $ dom-> createelement ('rss ');
$ Dom-> appendchild ($ RSS );

// Add attribute version = "2.0"
$ Rssver = $ dom-> createattribute ("version");
$ RSS-> appendchild ($ rssver );

$ Rssver_val = $ dom-> createtextnode ("2.0");
$ Rssver-> appendchild ($ rssver_val );

// Add a channel Element
$ Channel = $ dom-> createelement ('channel ');
$ RSS-> appendchild ($ channel );

// Add the title element and CDATA text node
$ Title = $ dom-> createelement ('title ');
$ Channel-> appendchild ($ title );
$ CDATA = $ dom-> createcdatasection ("why100000-oa");
$ Title-> appendchild ($ CDATA );

// Add the description element and CDATA text node
$ Description = $ dom-> createelement ('description ');
$ Channel-> appendchild ($ description );
$ CDATA = $ dom-> createcdatasection ("why100000-oa");
$ Description-> appendchild ($ CDATA );

// Add link elements and CDATA text nodes
$ Link = $ dom-> createelement ('link ');
$ Channel-> appendchild ($ link );
$ CDATA = $ dom-> createcdatasection ("http://why100000.com");
$ Link-> appendchild ($ CDATA );

// Add link elements and CDATA text nodes
$ Language = $ dom-> createelement ('language ');
$ Channel-> appendchild ($ language );
$ CDATA = $ dom-> createcdatasection ("ZH-CN");
$ Language-> appendchild ($ CDATA );

// Add the docs element and CDATA text node
$ Docs = $ dom-> createelement ('docs ');
$ Channel-> appendchild ($ docs );
$ CDATA = $ dom-> createcdatasection ("whydomain0.com document center");
$ Docs-> appendchild ($ CDATA );

// Add the generator element and CDATA text node
$ Generator = $ dom-> createelement ('generator ');
$ Channel-> appendchild ($ generator );
$ CDATA = $ dom-> createcdatasection ("RSS generator bywww. why100000.com");
$ Generator-> appendchild ($ CDATA );

// The following statement, the data is generally taken from the database
Create_item ("Topic-1", "http: // topic-1", "Description of topic-1", "author-1", "pubdate_data of topic-1");
Create_item ("Topic-2", "http: // topic-2", "Description of topic-2", "author-1", "pubdate_data of topic-2");
Create_item ("Topic-3", "http: // topic-3", "Description of topic-3", "author-1", "pubdate_data of topic-3");

// $ Text = $ dom-> createtextnode ('20140901 ');
// $ Channel-> appendchild ($ text );

// Output XML data
Echo $ dom-> savexml ();

/* Function */
Function create_item ($ title_data, $ link_data, $ description_data, $ author_data, $ pubdate_data)
{
Global $ Dom, $ channel;
// Add the item Element
$ Item = $ dom-> createelement ('item ');
$ Channel-> appendchild ($ item );

// Add the title element and CDATA text node
$ Title = $ dom-> createelement ('title ');
$ Item-> appendchild ($ title );
$ CDATA = $ dom-> createcdatasection ($ title_data );
$ Title-> appendchild ($ CDATA );

// Add link elements and CDATA text nodes
$ Link = $ dom-> createelement ('link ');
$ Item-> appendchild ($ link );
$ CDATA = $ dom-> createcdatasection ($ link_data );
$ Link-> appendchild ($ CDATA );

// Add the description element and CDATA text node
$ Description = $ dom-> createelement ('description ');
$ Item-> appendchild ($ description );
$ CDATA = $ dom-> createcdatasection ($ description_data );
$ Description-> appendchild ($ CDATA );

// Add the author element and CDATA text node
$ Author = $ dom-> createelement ('author ');
$ Item-> appendchild ($ author );
$ CDATA = $ dom-> createcdatasection ($ author_data );
$ Author-> appendchild ($ CDATA );

// Add the pubdate element and CDATA text node
$ Pubdate = $ dom-> createelement ('pubdate ');
$ Item-> appendchild ($ pubdate );
$ CDATA = $ dom-> createcdatasection ($ pubdate_data );
$ Pubdate-> appendchild ($ CDATA );
}
? > 〉

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.