php如何修改xml-创新互联

这篇文章将为大家详细讲解有关php如何修改xml,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联建站的客户来自各行各业,为了共同目标,我们在工作上密切配合,从创业型小企业到企事业单位,感谢他们对我们的要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。专业领域包括成都做网站、网站设计、电商网站开发、微信营销、系统平台开发。

php修改xml的方法:首先创建一个代码示例文件;然后通过“$new->nodeValue=$_content;”方法修改节点值即可。

php创建、增加、删除、修改xml

创建xml 方法:

 formatOutput = true;
 
$root = $doc -> createElement('root');//新建节点
 
$index = $doc -> createElement('index');//新建节点
 
$url = $doc -> createAttribute('url');//新建属性
$patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
$url -> appendChild($patch);//将$patch文本设为$url属性的值
 
$id = $doc -> createAttribute('id');
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
 
$title = $doc -> createAttribute('title');
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
 
$content = $doc -> createTextNode($_content);//节点值
 
$author = $doc -> createAttribute('author');
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
 
$sendtime = $doc -> createAttribute('time');
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
 
$index -> appendChild($id);//将$id设为index节点的属性,以下类同
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
 
$root -> appendChild($index);//设置index为root字节点
 
$doc -> appendChild($root);//设置root为跟节点
 
$doc -> save($xmlpatch);//保存文件
 
echo $xmlpatch . ' has create success';
 
?>

增加xml 的节点

  formatOutput = true; 
if($doc -> load($xmlpatch)) { 
$root = $doc -> documentElement;//获得根节点(root) 
$index = $doc -> createElement('index'); 
$url = $doc -> createAttribute('url'); 
$patch = $doc -> createTextNode($_htmlpatch); 
$url -> appendChild($patch); 
$id = $doc -> createAttribute('id'); 
$newsid = $doc -> createTextNode($_id); 
$id -> appendChild($newsid); 
$title = $doc -> createAttribute('title'); 
$newstitle = $doc -> createTextNode($_title); 
$title -> appendChild($newstitle); 
$content = $doc -> createTextNode($_content); 
$author = $doc -> createAttribute('author'); 
$newsauthor = $doc -> createTextNode($_author); 
$author -> appendChild($newsauthor); 
$sendtime = $doc -> createAttribute('time'); 
$newssendtime = $doc -> createTextNode($_sendtime); 
$sendtime -> appendChild($newssendtime); 
$index -> appendChild($id); 
$index -> appendChild($title); 
$index -> appendChild($content); 
$index -> appendChild($url); 
$index -> appendChild($author); 
$index -> appendChild($sendtime); 
$root -> appendChild($index); 
$doc -> save($xmlpatch); 
echo $_id . ' has been added in ' . $xmlpatch; 
} else { 
echo 'xml file loaded error!'; 
} 
?>

删除xml 的节点

 formatOutput = true; 
if($doc -> load($xmlpatch)) { 
$root = $doc -> documentElement; 
$elm = $root -> getElementsByTagName('index'); 
foreach ($elm as $new) { 
if($new -> getAttribute('id') == $_id) { 
if($root -> removeChild($new)) { 
echo $_id . ' has been deleted'; 
} else { 
echo $_id . ' delete failed'; 
} 
} 
} 
$doc -> save($xmlpatch); 
} else { 
echo 'xml file loaded error!'; 
} 
?>

修改XML的节点

  formatOutput = true; 
if($doc -> load($xmlpatch)) { 
$root = $doc -> documentElement; 
$elm = $root -> getElementsByTagName('index'); 
$checkexist = 0; 
foreach ($elm as $new) { 
if($new -> getAttribute('id') == $_id) { 
$new -> setAttribute('title', $_title); 
$new -> nodeValue = $_content;//修改节点值,真是太意外了,没想到跟JS一样直接能赋值... 
//$new -> removeChild($new -> nodevalue); 
$checkexist = 1; 
} 
} 
if($checkexist == 0) { 
echo $_id . ' is not found in ' . $xmlpatch; 
} else { 
$doc -> save($xmlpatch); 
echo $_id . ' has been changed'; 
} 
} else { 
echo 'xml file loaded error!'; 
} 
?>

关于php如何修改xml就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


当前标题:php如何修改xml-创新互联
转载来于:http://ybzwz.com/article/dpohed.html