PHP generates RSS files for improvement

Source: Internet
Author: User
PHP generates RSS files for improvement
I wrote a PHP code that uses the DOMDocument component to generate an RSS file, but it feels too bloated to encapsulate into a class but has never been successful.
Post-build RSS is basically 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: Small Wood





Of course the code is too redundant



$doc = new DOMDocument (' 1.0 ', ' utf-8 ');
$doc->formatoutput = true;


Create a label
Create RSS Labels
$rss = $doc->createelement (' RSS ');
Create a label below the channel
$channel = $doc->createelement (' channel ');
$ctitle = $doc->createelement (' title ');
$clink = $doc->createelement (' link ');
$cdescription = $doc->createelement (' description ');






foreach ($arr as $key = = $val) {
Create Item label
$item = $doc->createelement (' item ');
Create a child label label under item
$user = $doc->createelement (' user ');
$ititle = $doc->createelement (' title ');
$ilink = $doc->createelement (' link ');
$idescription = $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 where the database loop calls are needed */
Create content
Create the contents of the label below the item
$c _ititle = $doc->createtextnode ($val [' user_id ']);
$c _ilink = $doc->createtextnode (' http://www.xxx.com ');
$c _idescription = $doc->createtextnode (' ID: '. $val [' user_id ']. ', user_name: '. $val [' user_name ']. ', pass: '. $val [' Pass ']. ', Real_name: '. $val [' real_name ']);
Create the contents of the tag under 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 Item Property value
$a _id = $doc->createtextnode ($val [' user_id ']);


Creating properties
$attributes = $doc->createattribute (' id ');
/* Here is where the database loop calls are needed */
Item-level tag element content inheritance
$ititle->appendchild ($c _ititle);
$ilink->appendchild ($c _ilink);
$idescription->appendchild ($c _idescription);
element content inheritance for user-level tags
$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 where the database loop calls are needed */
Inherited
$channel->appendchild ($item);
Label inheritance for item-level labels
$item->appendchild ($user);
$item->appendchild ($ititle);
$item->appendchild ($ilink);
$item->appendchild ($idescription);
Id=1
$attributes->appendchild ($a _id);
//
$item->appendchild ($attributes);


Label inheritance for 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.