参考http://blog.csdn.net/k8080880/article/details/47616461
创建
<?php
$file = 'index.xml';
$idValue = "1";
$urlValue = 'http://www.baidu.com';
$contentValue = "百度";
$newIdValue = "2";
$newUrlValue = "http://google.cn";
$newContentValue = "谷歌";
$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;
$root = $doc -> createElement('root');//新建节点
$test = $doc -> createElement('test');//新建节点
$url = $doc -> createAttribute('url');//新建属性
$urlValueNode = $doc -> createTextNode($urlValue);//新建TEXT值
$url -> appendChild($urlValueNode);//将$patch文本设为$url属性的值
$id = $doc -> createAttribute('id');
$idValueNode = $doc -> createTextNode($idValue);
$id -> appendChild($idValueNode);
$content = $doc -> createTextNode($contentValue);//节点值
$test -> appendChild($url);//将$url设为index节点的属性,以下类同
$test -> appendChild($id);
$test -> appendChild($content);
$root -> appendChild($test);//设置index为root子节点
$doc -> appendChild($root);//设置root为跟节点
$doc -> save($file);//保存文件
echo '创建成功'."\n";
?>
增加
<?php
$file = 'index.xml';
$idValue = "1";
$urlValue = 'http://www.baidu.com';
$contentValue = "百度";
$newIdValue = "2";
$newUrlValue = "http://google.cn";
$newContentValue = "谷歌";
$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;
if($doc->load($file)) {
$root = $doc -> documentElement;//获得根节点(root)
$test = $doc -> createElement('test');
$url = $doc -> createAttribute('url');
$newUrlValueNode = $doc -> createTextNode($newUrlValue);
$url -> appendChild($newUrlValueNode);
$id = $doc -> createAttribute('id');
$newIdValueNode = $doc -> createTextNode($newIdValue);
$id -> appendChild($newIdValueNode);
$content = $doc -> createTextNode($newContentValue);//节点值
$test -> appendChild($url);
$test -> appendChild($id);
$test -> appendChild($content);
$root -> appendChild($test);
$doc -> save($file);
echo "添加成功"."\n";
}
?>
修改
<?php
$file = 'index.xml';
$idValue = "1";
$urlValue = 'http://www.baidu.com';
$contentValue = "百度";
$newIdValue = "2";
$newUrlValue = "http://google.cn";
$newContentValue = "谷歌";
$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;
if($doc->load($file)) {
$isExist = false;
$root = $doc -> documentElement;//获得根节点(root)
$elm = $root -> getElementsByTagName('test');
foreach ($elm as $item) {
if($item -> getAttribute('id') == $idValue) {
$item -> setAttribute('url', $newUrlValue);
$item -> nodeValue = $newContentValue;//修改节点值
$title = $doc -> createAttribute('title');//新建属性
$titleValueNode = $doc -> createTextNode("标题");//新建TEXT值
$title -> appendChild($titleValueNode);//将$patch文本设为$url属性的值
$item -> appendChild($title);
$isExist = true;
}
}
if(!$isExist) {
echo "没有找到节点";
} else {
$doc -> save($file);
echo "修改成功";
}
}
?>
删除
<?php
$file = 'index.xml';
$idValue = "1";
$urlValue = 'http://www.baidu.com';
$contentValue = "百度";
$newIdValue = "2";
$newUrlValue = "http://google.cn";
$newContentValue = "谷歌";
$doc = new DOMDocument('1.0', 'utf-8');
$doc -> formatOutput = true;
if($doc->load($file)) {
$root = $doc -> documentElement;//获得根节点(root)
$elm = $root -> getElementsByTagName('test');
foreach ($elm as $item) {
if($item -> getAttribute('id') == $newIdValue) {
if($root -> removeChild($item)) {
echo "删除成功";
} else {
echo '删除失败';
}
$item -> setAttribute('url', $idValue);
$item -> nodeValue = $contentValue;//修改节点值
//$item -> removeChild($item -> nodevalue);
}
}
$doc -> save($file);
}
?>