How PHP generates XML and explains XML

Source: Internet
Author: User
Tags php download
The next interview shows how to generate an XML file in the form of a program. If you have any questions, leave a message. Generate XML file download

To facilitate data processing, we sometimes need to consider storing data in XML format.

The next interview shows how to generate an XML file in the form of a program. If you have any questions, leave a message. Generate XML file download

 

PHP code
  1. /* Scheduled Tasks
  2. Blesslist -- generate a home page blessing list
  3. */
  4. // A class has been integrated here, which can be extracted and called separately.
  5. Class ListToXml {
  6. Protected $ db = null;
  7. // Initialize the database
  8. Protected function getCacheDB (){
  9. If (emptyempty ($ this-> db )){
  10. $ This-> db = new TMService ();
  11. }
  12. Return $ this-> db;
  13. }
  14. // Generate the blessing list data
  15. Function blesslist (){
  16. $ Db = $ this-> getCacheDB ();
  17. // Query the database
  18. $ Rs = $ db-> query ("select FCommentId, FType1, FDesQQ, FSrcQQ, FComment from Tbl_Comment ORDER by FCommentId DESC limit 20 ");
  19. // $ Rs = $ rs [0] ['cnt '];
  20. // $ Rs = $ rs? $ Rs: 0;
  21. $ This-> _ pubFile ('indexbless. XML', $ rs );
  22. }
  23. // Generate XML format. you can set it according to your own format. Note that the path should be changed to the desired path.
  24. Private function _ pubFile ($ fileName, $ data ){
  25. $ AHtml = '';
  26. If (is_array ($ data )){
  27. $ StrXML =" \ N ";
  28. Foreach ($ data as $ row ){
  29. $ Tmp = $ tpl;
  30. $ StrXML. = "\ n \ N ";
  31. $ StrXML. =" ". $ Row ['ftype1']." \ N ";
  32. $ StrXML. =" ". $ Row ['fdesqq']." \ N ";
  33. $ StrXML. =" ". $ Row ['fsrcqq']." \ N ";
  34. $ StrXML. =" ". $ Row ['fcomment']." \ N ";
  35. $ StrXML. ="";
  36. }
  37. $ AHtml = $ strXML. "\ n";
  38. } Else {
  39. $ AHtml = $ data;
  40. }
  41. $ Fo = @ fopen (CACHE_PATH. $ fileName, 'w ');
  42. @ Fwrite ($ fo, $ aHtml );
  43. @ Fclose ($ fo );
  44. If (file_exists (CACHE_PATH. $ fileName )){
  45. Return true;
  46. } Else {
  47. Return false;
  48. }
  49. }
  50. }

The generated XML data is as follows:

 

XML/HTML code
  1. 1
  2. 2131312
  3. 66073692
  4. The sky is not blue, because it is filled with happy colors; the wind is not quiet, because it is sending my thoughts; the hand is not cold, because your hands are still warm; the heart is not empty, because you have, in my heart!
  5. 1
  6. 123123
  7. 66073692
  8. The sky is not blue, because it is filled with happy colors; the wind is not quiet, because it is sending my thoughts; the hand is not cold, because your hands are still warm; the heart is not empty, because you have, in my heart!

Php has a buffer for reading XML documents, that is, the results obtained from multiple databases and only one data record are in a somewhat different format. in this case, xmlToArray must be processed independently. class. php download

PHP code
  1. // Read and parse XML. $ file is the XML file to be parsed.
  2. Function mblogXml ($ file ){
  3. $ Path = dirname (_ file _). "/library/xmlToArray. class. php"; // Set it based on your own path
  4. Include ($ path );
  5. $ Xml = new SofeeXmlParser ();
  6. $ Xml-> parseFile ($ file );
  7. $ Content = $ xml-> getTree ();
  8. Return $ content;
  9. }
  10. Print_r ();
  11. ?>

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.