从数据库到XML:使用PHP中DOM解析器生成XML树
时间:2005-11-15 来源:manbuzhe0301
使用XML DOM解析器创建XML文档树,
并读取数据库中一些记录填充文档树的叶子接点,
之后调用DomDocument->dump_mem()
输出XML文档树
create_element("RSS");
$root = $domdoc->append_child($root);
$rootattr = $domdoc->create_attribute("version", "2.0");
$rootattr = $root->append_child($rootattr);
$channel = $domdoc->create_element("CHANNEL");
$channel = $root->append_child($channel);
//item generate function;
function item_node($title_input, $date_input, $des_input) {
global $domdoc,$channel;
for ($i=0; $icreate_element("ITEM");
$item = $channel->append_child($item);
$title = $domdoc->create_element("TITLE");
$title_text = $domdoc->create_text_node($title_input[$i]);
$title = $item->append_child($title);
$title_text = $title->append_child($title_text);
$date = $domdoc->create_element("DATE");
$date_text = $domdoc->create_text_node($date_input[$i]);
$date = $item->append_child($date);
$date_text = $date->append_child($date_text);
$description = $domdoc->create_element("DESCRIPTION");
$des_text = $domdoc->create_text_node($des_input[$i]);
$description = $item->append_child($description);
$des_text = $description->append_child($des_text);
}
}
$title = array();
$date = array();
$description = array();
$link = mysql_connect("localhost", "root", "3955611");
mysql_select_db("xjyx");
$result = mysql_query("select * from news") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$title[] = $row['title'];
$date[] = $row['date'];
$description[] = $row['content'];
}
//print_r($title);die;
item_node($title, $date, $description);
echo $domdoc->dump_mem(1,"utf-8");
?>
并读取数据库中一些记录填充文档树的叶子接点,
之后调用DomDocument->dump_mem()
输出XML文档树
create_element("RSS");
$root = $domdoc->append_child($root);
$rootattr = $domdoc->create_attribute("version", "2.0");
$rootattr = $root->append_child($rootattr);
$channel = $domdoc->create_element("CHANNEL");
$channel = $root->append_child($channel);
//item generate function;
function item_node($title_input, $date_input, $des_input) {
global $domdoc,$channel;
for ($i=0; $icreate_element("ITEM");
$item = $channel->append_child($item);
$title = $domdoc->create_element("TITLE");
$title_text = $domdoc->create_text_node($title_input[$i]);
$title = $item->append_child($title);
$title_text = $title->append_child($title_text);
$date = $domdoc->create_element("DATE");
$date_text = $domdoc->create_text_node($date_input[$i]);
$date = $item->append_child($date);
$date_text = $date->append_child($date_text);
$description = $domdoc->create_element("DESCRIPTION");
$des_text = $domdoc->create_text_node($des_input[$i]);
$description = $item->append_child($description);
$des_text = $description->append_child($des_text);
}
}
$title = array();
$date = array();
$description = array();
$link = mysql_connect("localhost", "root", "3955611");
mysql_select_db("xjyx");
$result = mysql_query("select * from news") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$title[] = $row['title'];
$date[] = $row['date'];
$description[] = $row['content'];
}
//print_r($title);die;
item_node($title, $date, $description);
echo $domdoc->dump_mem(1,"utf-8");
?>
相关阅读 更多 +