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
-
- /* Scheduled Tasks
- Blesslist -- generate a home page blessing list
- */
- // A class has been integrated here, which can be extracted and called separately.
- Class ListToXml {
- Protected $ db = null;
- // Initialize the database
- Protected function getCacheDB (){
- If (emptyempty ($ this-> db )){
- $ This-> db = new TMService ();
- }
- Return $ this-> db;
- }
- // Generate the blessing list data
- Function blesslist (){
- $ Db = $ this-> getCacheDB ();
- // Query the database
- $ Rs = $ db-> query ("select FCommentId, FType1, FDesQQ, FSrcQQ, FComment from Tbl_Comment ORDER by FCommentId DESC limit 20 ");
- // $ Rs = $ rs [0] ['cnt '];
- // $ Rs = $ rs? $ Rs: 0;
- $ This-> _ pubFile ('indexbless. XML', $ rs );
- }
- // Generate XML format. you can set it according to your own format. Note that the path should be changed to the desired path.
- Private function _ pubFile ($ fileName, $ data ){
- $ AHtml = '';
- If (is_array ($ data )){
- $ StrXML =" \ N ";
- Foreach ($ data as $ row ){
- $ Tmp = $ tpl;
- $ StrXML. = "\ n \ N ";
- $ StrXML. =" ". $ Row ['ftype1']." \ N ";
- $ StrXML. =" ". $ Row ['fdesqq']." \ N ";
- $ StrXML. =" ". $ Row ['fsrcqq']." \ N ";
- $ StrXML. =" ". $ Row ['fcomment']." \ N ";
- $ StrXML. ="";
- }
- $ AHtml = $ strXML. "\ n";
- } Else {
- $ AHtml = $ data;
- }
- $ Fo = @ fopen (CACHE_PATH. $ fileName, 'w ');
- @ Fwrite ($ fo, $ aHtml );
- @ Fclose ($ fo );
- If (file_exists (CACHE_PATH. $ fileName )){
- Return true;
- } Else {
- Return false;
- }
- }
- }
The generated XML data is as follows:
XML/HTML code
-
-
-
- 1
- 2131312
- 66073692
- 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!
-
- 1
- 123123
- 66073692
- 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
-
- // Read and parse XML. $ file is the XML file to be parsed.
- Function mblogXml ($ file ){
- $ Path = dirname (_ file _). "/library/xmlToArray. class. php"; // Set it based on your own path
- Include ($ path );
- $ Xml = new SofeeXmlParser ();
- $ Xml-> parseFile ($ file );
- $ Content = $ xml-> getTree ();
- Return $ content;
- }
- Print_r ();
- ?>