这个小例子主要是对写了对Php对Xml文件的操作,和节点的遍历。

config.php文件

<?php
/*************************************************
/*
/*Author:Lee
/*Date  :2009-5-15
/*
************************************************
*/
$CONFIGS = array(
    
'title'=>"留言在线",
    
'version'=>"Copyright &copy; 2009 β1.0"    
);
?>

 

index.php文件

<?php
define("ROOT",$_SERVER['DOCUMENT_ROOT']);
/*************************************************
/*
/*Author:Lee
/*Date  :2009-5-15
/*
************************************************
*/
require(ROOT."/m/config.php");
$post = $_POST;
$data = ROOT."/m/data/message.xml";
if (empty($post))
{
    
$info = GetRecordFromXml($data);
}
else
{
    
$content = GetValues($post);
    WriteToXml(
$data,$content);
}

function GetRecordFromXml($fname)
{
    
$info = array();
    
$xml = new DOMDocument();
    
$xml->load($fname);
    
$i = 0;
    
foreach($xml->getElementsByTagName('record'as $list)
    {
        
$info[$i]['name'= $list->childNodes->item(0)->childNodes->item(0)->data;
        
$info[$i]['content'= $list->childNodes->item(1)->childNodes->item(0)->data;
        
$info[$i]['stime'= $list->childNodes->item(2)->childNodes->item(0)->data;
        
++$i;
    }
    
return $info;
}
function GetValues($values)
{
    date_default_timezone_set(
'Asia/Shanghai');
    
$name = $values['name'];
    
$content = $values['content'];
    
$_name = "<name>";
    
$name_ = "</name>";
    
$_content = "<content>";
    
$content_ = "</content>";
    
$stiem = "<stime>".date("Y-m-d H:i:s")."</stime></record>";
    
return '<record>'.$_name.$name.$name_.$_content.$content.$content_.$stiem.'</message>';
}
function WriteToXml($fname,$content)
{
    
$fp = fopen($fname,'r+');
    
fseek($fp,(0-strlen('</message>')),SEEK_END);
    
fwrite($fp,$content);
    
fclose($fp);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 
<HEAD>
  
<TITLE><?php echo $CONFIGS['title'];?></TITLE>
  
<META NAME="Author" CONTENT="Libch">
  
<META NAME="Keywords" CONTENT="">
  
<META NAME="Description" CONTENT="">
  
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
 
</HEAD>
    
<SCRIPT LANGUAGE="JavaScript">
    
<!--
        
String.prototype.trim = function()
        {
            
return this.replace(/(^\s*)|(\s*$)/g, "");
        }
        
function checkForm()
        {
            
var name= document.getElementById('name').value;
            
var content = document.getElementById('content').value;
            
if (name == null || name == "" || name.trim() == "")
            {
                alert(
"昵称不能为空!");
                
return ;
            }
            
if (content == null || content =="" || content.trim() =="")
            {
                alert(
"内容不能为空!");
                
return ;
            }
            document
.formMain.submit();
        }
    
//-->
    </SCRIPT>
    
<style type="text/css">
        
.style4{
            width
: 800px;
            height
: auto;
            margin
: 0px auto;
            margin
-bottom:20px;
            border
:1px solid #CCEFF5;
            background-color: #FAFCFD
        }
    
</style>
 
<BODY>
  
<FORM METHOD=POST ACTION="index.php" name="formMain">
    
<?php
    
if(empty($info))
    {
    
?>
    
<div class="style4" style="text-align:center;">还没有留言内容!</div>
    
<?php
    } 
else {
        
$size = sizeof($info);
    
?>
    
<div class="style4">
        
<?php 
        
for($i=0;$i<$size;++$i){    
        
?>
        
<TABLE>
        
<TR>
            
<TD>昵称:</TD>
            
<TD><?php echo $info[$i]['name'];?></TD>
        
</TR>
        
<TR>
            
<TD>内容:</TD>
            
<TD><?php echo $info[$i]['content'];?></TD>
        
</TR>
        
<TR>
            
<TD>时间:</TD>
            
<TD><?php echo $info[$i]['stime'];?></TD>
        
</TR>
        
</TABLE>
        
<?php
        }
        
?>
    
</div>
    
<?php
    }
    
?>
    
<div class="style4">
    
<TABLE>
        
<TR>
            
<TD>昵称:</TD>
            
<TD><INPUT TYPE="text" NAME="name" id="name" maxlength="10" size="20"><font color="red">*最多10个字符</font></TD>
        
</TR>
        
<TR>
            
<TD>内容:</TD>
            
<TD><TEXTAREA NAME="content" id="content" ROWS="3" COLS="50" maxlength="150"></TEXTAREA></TD>
        
</TR>
        
<TR>
            
<TD>&nbsp;</TD>
            
<TD><INPUT TYPE="button" value="提交" onclick="return checkForm();">&nbsp;<INPUT TYPE="reset"></TD>
        
</TR>
    
</TABLE>
    
</div>
    
<div class="style4" style="text-align:center;"><?php echo $CONFIGS['version'];?></div>
  
</FORM>
 
</BODY>
</HTML>

 位于data目录下的Xml文件结构

<?xml version="1.0" encoding="utf-8"?>
<message></message>

 

相关文章: