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: Then. . . This section is the source code for converting data from MYSQL into XML: while($row=mysql_fetch_array($rs)){ This section is the source code that fetches data from XML and converts it into HTML. . . function XML(){ function parse($data){ function tag_on($parser,$tag,$attributes){ function cdata($parser,$cdata){ function tag_off($parser,$tag){ $XML_parser = new XML(); Actually, you should be able to tell. . This example is a failure. . . Because the effect is not big. . You simply can't get what you want. . . well. . . bitter. . . .
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),
);
$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="n";
$XMLfile.="
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.="n";
$fp=fopen("XMLdoc/XMLdoc.XML", "w");
if(fwrite($fp,$XMLfile)){
echo "Write file successfully!";
}
else{
echo "Failed to write file !";
}
?>
class XML{
var $parser;
$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");
}
XML_parse($this->parser,$data);
}
if(XML_get_current_line_number($parser)==2){
echo "" . $tag . " ";
}
else{
switch ((XML_get_current_line_number($parser)-2)%6){
case 0 :
echo "Download ";
break;
case 1:
echo "ID number break;
case 2:
echo "Book title ";
break;
case 3:
echo "Author ";
break;
case 4:
echo "Publisher ";
break;
case 5:
echo "Publication Date ";
break;
}
}
}
echo $cdata;
}
echo "n";
}
}
$XMLfilename="XMLdoc/XMLdoc.XML";
$fp=fopen($XMLfilename,"r");
$XMLdata=fread($ fp,filesize($XMLfilename));
?>
$XML_parser ->parse($XMLdata);
?>