This stuff has been messing with me all day. . . But in the end, I found that I couldn't achieve the effect I imagined. . . What a shame. . . It seems that PHP needs to be strengthened for XML. . . If anyone has research on this. Welcome to write to discuss. . .
First create the table:
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 fetching data from MYSQL and converting it into 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 "Writing to file successfully!";
}
else{
echo "Failed to write file!";
}
?>
This section is the source code that fetches data from XML and converts it 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 number<td>";
break;
case 2:
echo "<tr><td>Book title<td>";
break;
case 3:
echo "<tr><td>Author<td>";
break;
case 4:
echo "<tr><td>Publishing House<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><head><title>Book Information</title></head>
<body>
<table border="0" cellspacing="1" cellpadding="2" width="80%" bgcolor="#b0d8fF" align="center">
<?
$XML_parser->parse($XMLdata);
?>
</table>
In fact, you should be able to see it. . This example is a failure. . . Because the effect is not big. . You simply can't get what you want. . . well. . . bitter. . . .