This stuff took me a day... However, at the end, I found that I could not achieve the effect I imagined... It's a pity... It seems that PHP still needs to strengthen XML... If anyone has research on this. Thank you for writing to us...
Create a table first:
Create table books (
Bookid int (4) not null auto_increment,
Bookname varchar (100) not null,
Bookauth varchar (50) not null,
Bookpublisher varchar (50) not null,
Bookpubdate datetime not null,
Bookurl varchar (50) not null,
KEY bookid (bookid ),
);
Then... This section is the source code for converting data from MYSQL to XML:
<?
$ Connect_id = mysql_connect ("localhost", "root ","");
Mysql_select_db ("bbs", $ connect_id );
$ Query = "select * from books order by bookid ";
$ Rs = mysql_query ($ query, $ connect_id );
$ Numfields = mysql_num_fields ($ rs );
$ XMLfile = "<? XML version = "1.0" encoding = "GB2312"?> N ";
$ XMLfile. = "<books> n ";
While ($ row = mysql_fetch_array ($ rs )){
For ($ I = 0; $ I <$ numfields; $ I ){
$ Fieldname = mysql_field_name ($ rs, $ I );
$ XMLfile. = "<". $ fieldname. ">". $ row [$ I]. "</". $ fieldname. "> n ";
}
}
Mysql_free_result ($ rs );
Mysql_close ($ connect_id );
$ XMLfile. = "</books> n ";
$ Fp = fopen ("XMLdoc/XMLdoc. XML", "w ");
If (fwrite ($ fp, $ XMLfile )){
Echo "file written! ";
}
Else {
Echo "failed to write file! ";
}
?>
This section is the source code for converting data from XML into HTML...
<?
Class XML {
Var $ parser;
Function XML (){
$ This-> parser = XML_parser_create ();
XML_set_object ($ this-> parser, & $ this );
XML_set_element_handler ($ this-> parser, "tag_on", "tag_off ");
XML_set_character_data_handler ($ this-> parser, "cdata ");
}
Function parse ($ data ){
XML_parse ($ this-> parser, $ data );
}
Function tag_on ($ parser, $ tag, $ attributes ){
If (XML_get_current_line_number ($ parser) = 2 ){
Echo "<tr> <td colspan =" 2 "align =" center ">". $ tag. "</td> ";
}
Else {
Switch (XML_get_current_line_number ($ parser)-2) % 6 ){
Case 0:
Echo "<tr> <td> download <td> ";
Break;
Case 1:
Echo "<tr> <td> id <td> ";
Break;
Case 2:
Echo "<tr> <td> title <td> ";
Break;
Case 3:
Echo "<tr> <td> author <td> ";
Break;
Case 4:
Echo "<tr> <td> press <td> ";
Break;
Case 5:
Echo "<tr> <td> publication date <td> ";
Break;
}
}
}
Function cdata ($ parser, $ cdata ){
Echo $ cdata;
}
Function tag_off ($ parser, $ tag ){
Echo "n ";
}
}
$ XML_parser = new XML ();
$ XMLfilename = "XMLdoc/XMLdoc. XML ";
$ Fp = fopen ($ XMLfilename, "r ");
$ XMLdata = fread ($ fp, filesize ($ XMLfilename ));
?>
<HTML> <Body>
<Table border = "0" cellspacing = "1" cellpadding = "2" width = "80%" bgcolor = "# b0d8fF" align = "center">
<?
$ XML_parser-> parse ($ XMLdata );
?>
</Table>
Actually, you can see that .. This example is a failure... Because it is not very effective .. You cannot get what you want... Alas... Bitter ....