Home  >  Article  >  Backend Development  >  Use PHP to dynamically generate xml files and extract data from xml files and convert them into html_PHP tutorial

Use PHP to dynamically generate xml files and extract data from xml files and convert them into html_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:22963browse

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 converting data from MYSQL 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="n";
$XMLfile.="n";

while($row=mysql_fetch_array($rs)){
for($i=0;$i<$numfields;$i ){
$fieldname=mysql_field_name($rs,$i);
$XMLfile.="<" . $fieldname . ">" . $row[$i] . "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 !";
}
?>

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 "" . $tag . "";
}
else{
switch ((XML_get_current_line_number($parser)-2)%6){
case 0 :
echo "Download";
break;
case 1:
echo "ID numberbreak;
case 2:
echo "Book title";
break;
case 3:
echo "Author";
break;
case 4:
echo "Publisher";
break;
case 5:
echo "Publication Date";
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));
?>
Book information


$XML_parser ->parse($XMLdata);
?>

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. . . .

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508362.htmlTechArticleThis stuff bothered 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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn