Improve the rss file generated by php

Source: Internet
Author: User
I wrote a php code that uses the DOMDocument & nbsp; component to generate an rss file. However, the code is too bloated and I want to encapsulate it into a class but it has never been successful, the generated rss is generally like this & lt; rss & nbsp; version = "2.0" & gt; & lt; channel & gt; & lt; improve the rss file generation by item & n php
I wrote a php code that uses the DOMDocument component to generate an rss file, but it feels too bloated. I want to encapsulate it into a class but it has never been successful. please refer
The generated rss is generally like this.





1 123 Wood
1
Http://www.xxx.com
Id: 1, user_name:, pass: 123, real_name: Wood




2 456 Small Wood
2
Http://www.xxx.com
Id: 2, user_name:, pass: 456, real_name: xiaowood





Of course the code is too redundant



$ Doc = new DOMDocument ('1. 0', 'utf-8 ');
$ Doc-> formatOutput = true;


// Create a tag
// Create an rss label
$ Rss = $ doc-> createElement ('RSS ');
// Create a tag under the channel
$ Channel = $ doc-> createElement ('channel ');
$ Ctitle = $ doc-> createElement ('title ');
$ Clink = $ doc-> createElement ('link ');
$ Cdescription = $ doc-> createElement ('Description ');






Foreach ($ arr as $ key => $ val ){
// Create an item tag
$ Item = $ doc-> createElement ('ITEM ');
// Create a sub-label under item
$ User = $ doc-> createElement ('user ');
$ Ititle = $ doc-> createElement ('title ');
$ Ilink = $ doc-> createElement ('link ');
$ Ideelement = $ doc-> createElement ('Description ');
// Create a user tag
$ User_id = $ doc-> createElement ('User _ id ');
$ User_name = $ doc-> createElement ('User _ name ');
$ User_pass = $ doc-> createElement ('User _ pass ');
$ Real_name = $ doc-> createElement ('Real _ name ');
/* Here is the place where the database needs to be called cyclically */
// Create content
// Create the tag content under the item
$ C_ititle = $ doc-> createTextNode ($ val ['User _ id']);
$ C_ilink = $ doc-> createTextNode ('http: // www.xxx.com ');
$ C_ide.pdf = $ doc-> createTextNode ('Id :'. $ val ['User _ id']. ', user_name :'. $ val ['User _ name']. ', pass :'. $ val ['pass']. ', real_name :'. $ val ['real _ name']);
// Create the tag content under the user
$ C_user_id = $ doc-> createTextNode ($ val ['User _ id']);
$ C_user_name = $ doc-> createTextNode ($ val ['User _ name']);
$ C_user_pass = $ doc-> createTextNode ($ val ['pass']);
$ C_real_name = $ doc-> createTextNode ($ val ['real _ name']);
// Create the item property value
$ A_id = $ doc-> createTextNode ($ val ['User _ id']);


// Create attributes
$ Attributes = $ doc-> createAttribute ('id ');
/* Here is the place where the database needs to be called cyclically */
// Inherit the element content of the item-level label
$ Ititle-> appendChild ($ c_ititle );
$ Ilink-> appendChild ($ c_ilink );
$ IDevice-> appendChild ($ c_idevice );
// Inherit the element content of the user-level label
$ User_id-> appendChild ($ c_user_id );
$ User_name-> appendChild ($ c_user_name );
$ User_pass-> appendChild ($ c_user_pass );
$ Real_name-> appendChild ($ c_real_name );
/* Here is the place where the database needs to be called cyclically */
// Inherit
$ Channel-> appendChild ($ item );
// Tag inheritance of item-level labels
$ Item-> appendChild ($ user );
$ Item-> appendChild ($ ititle );
$ Item-> appendChild ($ ilink );
$ Item-> appendChild ($ ide.pdf );
// Id = 1
$ Attributes-> appendChild ($ a_id );
//
$ Item-> appendChild ($ attributes );


// Tag inheritance of item-level labels
$ User-> appendChild ($ user_id );
$ User-> appendChild ($ user_name );
$ User-> appendChild ($ user_pass );
$ User-> appendChild ($ real_name );
}

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.