• 技术文章 >后端开发 >PHP问题

    php修改xml的方法

    藏色散人藏色散人2020-11-20 09:59:00原创552

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

    php入门到就业线上直播课:进入学习

    推荐:《PHP视频教程

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

    创建xml 方法:

    <?php
    $xmlpatch = 'index.xml';
    $_id = '1';
    $_title = 'title1';
    $_content = 'content1';
    $_author = 'author1';
    $_sendtime = 'time1';
    $_htmlpatch = '1.html';
     
    $doc = new DOMDocument('1.0', 'utf-8');
    $doc -> 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 的节点

     <?php 
    $xmlpatch = 'index.xml'; 
    $_id = '3'; 
    $_title = 'title3'; 
    $_content = 'content3'; 
    $_author = 'author3'; 
    $_sendtime = 'time3'; 
    $_htmlpatch = '3.html'; 
    $doc = new DOMDocument(); 
    $doc -> 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 的节点

    <?php 
    $xmlpatch = 'index.xml'; 
    $_id = '3'; 
    $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!'; 
    } 
    ?>

    修改XML的节点

     <?php 
    $xmlpatch = 'index.xml'; 
    $_id = '2'; 
    $_title = 'has been changed'; 
    $_content = 'has been changed'; 
    $doc = new DOMDocument(); 
    $doc -> 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中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:PHP 修改xml
    上一篇:php session 取不到值怎么办 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• php 字符串如何转换成xml• xml如何添加注释?• 教你分割WordPress导出的XML文件• php如何将数组转xml字符串
    1/1

    PHP中文网