简单的xml留言簿

 1   
 2/** 加载 domxml 扩展 **/   
 3if(!extension_loaded("domxml"))   
 4dl("php_domxml.dll");   
 5  
 6/** 加载 iconv 扩展 **/   
 7if(!extension_loaded("iconv"))   
 8dl("php_iconv.dll");   
 9/*   
10echo "

<pre>准 备\n\n";
echo "加载 domxml 扩展后可使用以下函数\n";
print_r(get_extension_funcs("domxml"));
echo "\n加载 iconv 扩展后可使用以下函数\n";
print_r(get_extension_funcs("iconv"));
*/

/** 初始化 **/
$boardname = "board.xml";
if(! file_exists($boardname)) {
$xmlstr = "<?xml version='1.0' encoding='GB2312'

 1$fp = fopen($boardname,"w");   
 2fwrite($fp,$xmlstr);   
 3fclose($fp);   
 4xml_insert($boardname,array(name=&gt;"测试",content=&gt;"简单的留言簿\n主要是演示domxml的应用\n用xml文件保存信息"));   
 5}   
 6  
 7/** 连接xml文档 **/   
 8function xml_connect($xmlfiale) {   
 9$doc = xmldocfile($xmlfiale);   
10return xpath_new_context($doc);   
11}   
12  
13/** 插入数据 **/   
14function xml_insert($xmlfile,$xmlarray) {   
15$doc = xmldocfile($xmlfile);   
16$f = xpath_eval(xpath_new_context($doc), "//*/row");   
17$root = $doc-&gt;root();   
18$new = $root-&gt;new_child("row");   
19$new-&gt;new_child("id",count($f-&gt;nodeset));   
20$new-&gt;new_child("date",date("Y-m-d H:i:s"));   
21foreach($xmlarray as $key=&gt;$value) {   
22$new-&gt;new_child($key,iconv("GB2312","UTF-8",$value));   
23}   
24$fp = fopen($xmlfile,"w");   
25fwrite($fp,$doc-&gt;dumpmem());   
26fclose($fp);   
27}   
28  
29/** 删除数据 **/   
30function xml_delete($xmlfile,$query) {   
31$doc = xmldocfile($xmlfile);   
32$f = xpath_eval(xpath_new_context($doc), "//*/row[$query]");   
33if(count($f-&gt;nodeset)) {   
34for($i=0;$i<count($f->nodeset);$i++)   
35$f-&gt;nodeset[0]-&gt;unlink_node();   
36$fp = fopen($xmlfile,"w");   
37fwrite($fp,$doc-&gt;dumpmem());   
38fclose($fp);   
39}   
40}   
41  
42/** 执行查询 **/   
43function xml_query($conn,$query) {   
44$found = xpath_eval($conn, $query);   
45$result = $found-&gt;nodeset;   
46krsort($result);   
47if(count($result))   
48return $result;   
49return array();   
50}   
51  
52/** 返回查询 **/   
53function xml_fetch_row(&amp;$result) {   
54list($key,$value) = each($result);   
55if(empty($value)) return 0;   
56foreach($value-&gt;children() AS $node) {   
57$v = $node-&gt;children();   
58if($node-&gt;tagname)   
59$ar[$node-&gt;tagname] = nl2br(iconv("UTF-8","GB2312",$v[0]-&gt;content));   
60}   
61return $ar;   
62}   
63  
64if($_POST['submit']) {   
65xml_insert($boardname,array(name=&gt;$_POST['name'],content=&gt;$_POST['content']));   
66}   
67?&gt;   
68  
69<form action="" method="post">   
70姓名<input name="name" type="text"/><input name="submit" type="submit" value="留言"/><br/>   
71留言<br/>   
72<textarea cols="60" name="content" rows="4"></textarea>   
73</form>   
74  

$conn = xml_connect($boardname);
$result = xml_query($conn, "//*/row[id&gt;=0]");
echo "<table bgcolor="#d0d0d0" border="0" cellpadding="0" cellspacing="1" rules="rows" width="80%">";
while($row = xml_fetch_row($result)) {
echo "<tr bgcolor="#d0d0d0"><td width="10%">编号:</td><td width="10%">$row[id]</td><td width="10%">来源:</td><td>$row[name]</td>";
echo "<td align="right" width="10%">时间:</td><td align="right" nowrap="" width="10%">$row[date]</td></tr>\n";
echo "<tr bgcolor="#eeeeee"><td colspan="6">$row[content]</td></tr><tr bgcolor="#e0e0e0"></tr>\n";
}
echo "</table>";

 1  
 2\---------------------------------------------------------------   
 3  
 4好东西就要支持~   
 5\---------------------------------------------------------------   
 6  
 7支持!   
 8\---------------------------------------------------------------   
 9  
10test   
11\---------------------------------------------------------------   
12  
13Warning: dl() [function.dl]: Unable to load dynamic library './php_domxml.dll' - 找不到指定的模块。 in D:\WEB\xml\test.php on line 4   
14  
15Warning: dl() [function.dl]: Unable to load dynamic library './php_iconv.dll' - 找不到指定的模块。 in D:\WEB\xml\test.php on line 8   
16  
17Fatal error: Call to undefined function: xmldocfile() in D:\WEB\xml\test.php on line 36   
18  
19\---------------------------------------------------------------   
20  
21linux下可以吗?是不是编译php的时候要加上with-xml参数?   
22\---------------------------------------------------------------   
23  
24大力支持   
25  
26\---------------------------------------------------------------   
27  
28谢谢 xuzuning(唠叨)   
29\---------------------------------------------------------------   
30  
31厉害,   
32我一直也在研究php和xml的结合问题   
33\---------------------------------------------------------------   
34  
3536\---------------------------------------------------------------   
37  
38谢谢,研究中……</count($f-></pre>
Published At
Categories with Web编程
comments powered by Disqus