Home >Backend Development >PHP Tutorial >Use PHP to dynamically generate xml files and extract data from xml files and convert them into html small programs_PHP tutorial

Use PHP to dynamically generate xml files and extract data from xml files and convert them into html small programs_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:31:581058browse

这个东东搞了我一天。。。可是到最后发现却无法实现自己想像中的那种效果。。。真是可惜。。。看来php(做为现在的主流开发语言)对xml(标准化越来越近了)还需要加强。。。如果哪位对此有研究。欢迎来信探讨。。。

首先建表:
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),
);

然后。。。这段是从MySQL(和PHP搭配之最佳组合)里取数据转化成xml(标准化越来越近了)的源代码:
$connect_id=MySQL(和PHP搭配之最佳组合)_connect("localhost","root","");
MySQL(和PHP搭配之最佳组合)_select_db("bbs",$connect_id);
$query="select * from books order by bookid";
$rs=MySQL(和PHP搭配之最佳组合)_query($query,$connect_id);
$numfields=MySQL(和PHP搭配之最佳组合)_num_fields($rs);
$xml(标准化越来越近了)file="(标准化越来越近了) version="1.0" encoding="GB2312"?>n";
$xml(标准化越来越近了)file.="n";

while($row=MySQL(和PHP搭配之最佳组合)_fetch_array($rs)){
for($i=0;$i<$numfields;$i ){
$fieldname=MySQL(和PHP搭配之最佳组合)_field_name($rs,$i);
$xml(标准化越来越近了)file.="<" . $fieldname . ">" . $row[$i] . "n";
}
}
MySQL(和PHP搭配之最佳组合)_free_result($rs);
MySQL(和PHP搭配之最佳组合)_close($connect_id);
$xml(标准化越来越近了)file.="n";
$fp=fopen("xml(标准化越来越近了)doc/xml(标准化越来越近了)doc.xml(标准化越来越近了)","w");
if(fwrite($fp,$xml(标准化越来越近了)file)){
echo "写入文件成功!";
}
else{
echo "写入文件失败!";
}
?>

这段是从xml(标准化越来越近了)里取数据转化成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 "下载";
break;
case 1:
echo "ID号";
break;
case 2:
echo "书名";
break;
case 3:
echo "作者";
break;
case 4:
echo "出版社";
break;
case 5:
echo "出版日期";
break;
}
}
}

function cdata($parser,$cdata){
echo $cdata;
}

function tag_off($parser,$tag){
echo "n";
}
}

$xml(Standardization is getting closer)_parser = new xml(Standardization is getting closer)();
$xml(Standardization Getting closer)filename="xml(Standardization is getting closer)doc/xml(Standardization is getting closer)doc.xml (Standardization is getting closer)";
$fp=fopen($xml(Standardization is getting closer)filename,"r");
$xml(Standardization is getting closer)data=fread($fp,filesize($xml(Standardization is getting closer)filename));
?>
Book information


$xml(Standardization is getting closer)_parser-> ;parse($xml(Standardization is getting closer)data);
?>

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508890.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 (as the current mainstream development language) is suitable for...
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