參考http://blog.csdn.net/k8080880/article/details/47616461
創(chuàng)建
<?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');//新建節(jié)點(diǎn)
$test = $doc -> createElement('test');//新建節(jié)點(diǎn)
$url = $doc -> createAttribute('url');//新建屬性
$urlValueNode = $doc -> createTextNode($urlValue);//新建TEXT值
$url -> appendChild($urlValueNode);//將$patch文本設(shè)為$url屬性的值
$id = $doc -> createAttribute('id');
$idValueNode = $doc -> createTextNode($idValue);
$id -> appendChild($idValueNode);
$content = $doc -> createTextNode($contentValue);//節(jié)點(diǎn)值
$test -> appendChild($url);//將$url設(shè)為index節(jié)點(diǎn)的屬性岸晦,以下類同
$test -> appendChild($id);
$test -> appendChild($content);
$root -> appendChild($test);//設(shè)置index為root子節(jié)點(diǎn)
$doc -> appendChild($root);//設(shè)置root為跟節(jié)點(diǎn)
$doc -> save($file);//保存文件
echo '創(chuàng)建成功'."\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;//獲得根節(jié)點(diǎn)(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);//節(jié)點(diǎn)值
$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;//獲得根節(jié)點(diǎn)(root)
$elm = $root -> getElementsByTagName('test');
foreach ($elm as $item) {
if($item -> getAttribute('id') == $idValue) {
$item -> setAttribute('url', $newUrlValue);
$item -> nodeValue = $newContentValue;//修改節(jié)點(diǎn)值
$title = $doc -> createAttribute('title');//新建屬性
$titleValueNode = $doc -> createTextNode("標(biāo)題");//新建TEXT值
$title -> appendChild($titleValueNode);//將$patch文本設(shè)為$url屬性的值
$item -> appendChild($title);
$isExist = true;
}
}
if(!$isExist) {
echo "沒有找到節(jié)點(diǎn)";
} 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;//獲得根節(jié)點(diǎn)(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;//修改節(jié)點(diǎn)值
//$item -> removeChild($item -> nodevalue);
}
}
$doc -> save($file);
}
?>