Improvement on generating rss files in php-php Tutorial

Source: Internet
Author: User
I wrote a php code that uses the DOMDocument 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.





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




2 456 Small Wood
2
Http://www.xxx.com </link>
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 );
}




// Create content
// Create the tag content under the channel
$ C_ctitle = $ doc-> createTextNode ('test rss ');
$ C_clink = $ doc-> createTextNode ('http: // www.xxx.com ');
$ C_cdescription = $ doc-> createTextNode ('This is a test rss ');
// Create an rss version attribute value
$ Rss_attribute_c = $ doc-> createTextNode ('2. 0 ');


// Create an rss version attribute
$ Rss_attribute = $ doc-> createAttribute ('version ');


// Inherit
// Element content inheritance of channel-level labels
$ Ctitle-> appendChild ($ c_ctitle );
$ Clink-> appendChild ($ c_clink );
$ Cdescription-> appendChild ($ c_cdescription );
// Version = "2.0"
$ Rss_attribute-> appendChild ($ rss_attribute_c );




// Channel-level label inheritance
$ Channel-> appendChild ($ ctitle );
$ Channel-> appendChild ($ clink );
$ Channel-> appendChild ($ cdescription );








// Create the root node
$ Rss-> appendChild ($ channel );
$ Rss-> appendChild ($ rss_attribute );
$ Doc-> appendChild ($ rss );


// Save the xml file
$ Doc-> save ('Hello. XML ');


Function connect_ SQL (){
$ Con = mysql_connect ('localhost', 'root', '000000') or die ('MySQL error: '. mysql_error ());
Mysql_select_db ('test ');
Mysql_query ('set names utf8 ');
}
Function getall (){
$ SQL = "SELECT * FROM test ";
$ Query = mysql_query ($ SQL );
$ Content = array ();
While ($ row = mysql_fetch_array ($ query )){
$ Content [] = $ row;
}
Return $ content;
}


I have tried it and I have never been able to convert it into a class.


Reply to discussion (solution)

Why dom?
Is it okay to assemble strings directly? For example

$ar = array(  array(    'id' => 1,    'user' => array('user_id' => 1, 'user_name' => '', 'user_pass' => 123, 'real_name' => 'aa'),     'title' => 1,    'link' => 'http://www.xxx.com',    'description' => 'id:1,user_name:,pass:123,real_name:aa',  ),  array(    'id' => 2,    'user' => array('user_id' => 2, 'user_name' => '', 'user_pass' => 456, 'real_name' => 'bb'),     'title' => 2,    'link' => 'http://www.xxx.com',    'description' => 'id:1,user_name:,pass:456,real_name:bb',  ),);
    
     
      
       1
      
      
       1
       http://www.xxx.com</link>
       
        id:1,user_name:,pass:123,real_name:aa
       
      
     
     
      
       2
      
      
       2
       http://www.xxx.com</link>
       
        id:1,user_name:,pass:456,real_name:bb
       
      
     
    

Why dom?
Is it okay to assemble strings directly? For example

$ar = array(  array(    'id' => 1,    'user' => array('user_id' => 1, 'user_name' => '', 'user_pass' => 123, 'real_name' => 'aa'),     'title' => 1,    'link' => 'http://www.xxx.com',    'description' => 'id:1,user_name:,pass:123,real_name:aa',  ),  array(    'id' => 2,    'user' => array('user_id' => 2, 'user_name' => '', 'user_pass' => 456, 'real_name' => 'bb'),     'title' => 2,    'link' => 'http://www.xxx.com',    'description' => 'id:1,user_name:,pass:456,real_name:bb',  ),);
    
     
      
       1
      
      
       1
       http://www.xxx.com</link>
       
        id:1,user_name:,pass:123,real_name:aa
       
      
     
     
      
       2
      
      
       2
       http://www.xxx.com</link>
       
        id:1,user_name:,pass:456,real_name:bb
       
      
     
    

Directly output the echo, and then write fwrite to an xml file. this is a good way, but I think php has its own domdocument component, so it is not clear whether it has its advantages over directly piecing together to generate a file.

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.