• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)

php 搞代码 4年前 (2022-01-01) 35次浏览 已收录 0个评论

PHP写XML方法很多,这里主要介绍一下DOMDocument的用法,跟 JS大体上相同,其实非常简单。

共分四个文件,分别是创建、增加、删除、修改四个功能,变量都是写死的,改一改用$_POST方式接收就可以用了
//index.php 创建功能

代码如下:
<?php
$xmlpatch = ‘index.xml’;
$_id = ‘1’;
$_title = ‘title1’;
$_content = ‘content1’;
$_author = ‘author1’;
$_sendtime = ‘time1’;
$_htmlpatch = ‘1.html’;
gaodaima.com$doc = new DOMDo来源[email protected]搞@^&代*@码网cument(‘1.0’, ‘utf-8’);
$doc -> formatOutput = true;
gaodaima.com$root = $doc -> createElement(‘root’);//新建节点
gaodaima.com$index = $doc -> createElement(‘index’);//新建节点
gaodaima.com$url = $doc -> createAttribute(‘url’);//新建属性
$patch = $doc -> createTextNode($_htmlpatch);//新建TEXT值
$url -> appendChild($patch);//将$patch文本设为$url属性的值
gaodaima.com$id = $doc -> createAttribute(‘id’);
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
gaodaima.com$title = $doc -> createAttribute(‘title’);
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
gaodaima.com$content = $doc -> createTextNode($_content);//节点值
gaodaima.com$author = $doc -> createAttribute(‘author’);
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
gaodaima.com$sendtime = $doc -> createAttribute(‘time’);
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
gaodaima.com$index -> appendChild($id);//将$id设为index节点的属性,以下类同
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
gaodaima.com$root -> appendChild($index);//设置index为root字节点
gaodaima.com$doc -> appendChild($root);//设置root为跟节点
gaodaima.com$doc -> save($xmlpatch);//保存文件
gaodaima.comecho $xmlpatch . ‘ has create success’;
gaodaima.com?>
gaodaima.com

XML操作

gaodaima.com

//add.php 增加功能(跟index.php文件差不多,主要就是加个load载入跟 $root = $doc -> documentElement获得跟节点

代码如下:
<?php
$xmlpatch = ‘index.xml’;
$_id = ‘2’;
$_title = ‘title2’;
$_content = ‘content2’;
$_author = ‘author2’;
$_sendtime = ‘time2’;
$_htmlpatch = ‘2.html’;
gaodaima.com$doc = new DOMDocument();
$doc -> formatOutput = true;
if($doc -> load($xmlpatch)) {
$root = $doc -> documentElement;//获得根节点(root)
$index = $doc -> createElement(‘index’);
gaodaima.com$url = $doc -> createAttribute(‘url’);
$patch = $doc -> createTextNode($_htmlpatch);
$url -> appendChild($patch);
gaodaima.com$id = $doc -> createAttribute(‘id’);
$newsid = $doc -> createTextNode($_id);
$id -> appendChild($newsid);
gaodaima.com$title = $doc -> createAttribute(‘title’);
$newstitle = $doc -> createTextNode($_title);
$title -> appendChild($newstitle);
gaodaima.com$content = $doc -> createTextNode($_content);
gaodaima.com$author = $doc -> createAttribute(‘author’);
$newsauthor = $doc -> createTextNode($_author);
$author -> appendChild($newsauthor);
gaodaima.com$sendtime = $doc -> createAttribute(‘time’);
$newssendtime = $doc -> createTextNode($_sendtime);
$sendtime -> appendChild($newssendtime);
gaodaima.com$index -> appendChild($id);
$index -> appendChild($title);
$index -> appendChild($content);
$index -> appendChild($url);
$index -> appendChild($author);
$index -> appendChild($sendtime);
gaodaima.com$root -> appendChild($index);
gaodaima.com$doc -> save($xmlpatch);
gaodaima.comecho $_id . ‘ has been added in ‘ . $xmlpatch;
gaodaima.com} else {
echo ‘xml file loaded error!’;
}
?>

XML操作-添加

gaodaima.com

//edit.php 修改功能(这里只修改title属性值 跟节点值)

代码如下:
<?php
$xmlpatch = ‘index.xml’;
$_id = ‘2’;
$_title = ‘has been changed’;
$_content = ‘has been changed’;
gaodaima.com$doc = new DOMDocument();
$doc -> formatOutput = true;
gaodaima.comif($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!’;
}
gaodaima.com?>

XML操作-修改

gaodaima.com

//del.php 删除功能

代码如下:
<?php
$xmlpatch = ‘index.xml’;
$_id = ‘2’;
gaodaima.com$doc = new DOMDocument();
$doc -> 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!’;
}
gaodaima.com?>

XML操作-删除

gaodaima.com

gaodaima.com
总结一下,创建跟添加主要用的就是create跟appendChild,create后边跟Element就是创建节点,跟Attribute就是创建属性,TextNode就是创建值,然后appendChild就是设置从属关系,这么一看非常简单。删除与修改都是用先获得节点列表getElementsByTagName然后foreach遍历想要修改的节点.

以上就是php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php中DOMDocument简单用法示例代码(XML创建、添加、删除、修改)

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址