php对xml文件的增删改查操作实现方法分析
本文实例讲述了php对xml文件的增删改查操作实现方法。分享给大家供大家参考,具体如下:
xml源文件
2016-08-01 文件内容
$file='callBoard.xml';
查询xml:
$doc=newDOMDocument();//实例化对像
$doc->load($file);//载入文件
$data=array();
$time=$doc->getElementsByTagName("endTime");//获取元素是title
$dateTime=$time->item(0)->nodeValue;//对指定元素获取值
$content=$doc->getElementsByTagName("content");//获取元素是title
$dataContent=$content->item(0)->nodeValue;//对指定元素获取值
更新和创建:
if(file_exists($file))//判断文件是否存在
{
//如果存在就更新内容
$doc=newDOMDocument();//实例化对像
$doc->load($file);//载入文件
$resultDate=$doc->getElementsByTagName("endTime");//获取元素是dateNumber
$resultDate->item(0)->nodeValue=$_POST['time'];//对指定元素赋值
$resultBall1=$doc->getElementsByTagName("content");//获取元素是ball1
$resultBall1->item(0)->nodeValue=$_POST['content'];//对指定元素赋值
$doc->save($file);//当有用到修改时才用此方法
}
else
{
//如果不存在就创建文件
$doc=newDOMDocument('1.0','utf-8');//申明是XML
$doc->formatOutput=true;//格式输出
$root=$doc->createElement('root');//创建根元素
$endTime=$doc->createElement("endTime",$_POST['time']);//创建元素dateNumber并赋值$arr['resultDate']内容
$content=$doc->createElement("content",$_POST['content']);//创建元素ball1并赋值$arr['resultBall1']内容
$root->appendChild($endTime);//$content元素包含$dateNumber元素
$root->appendChild($content);//$content元素包含$ball1元素
$doc->appendChild($root);//结束建立根元索
$doc->save($file);//生成xml.xml文件
}
PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下