php 读写xml 修改xml

需要修改的xml

<?xml version="1.0"?>
<config>
    <administration>
        <adminuser>
            <user>
                <id type="autoinc">1</id>
                <name type="username" min="3" max="20">admin</name>
                <passwdmd5 type="passwd" min="4" max="10">21232f297a57a5a743894a0e4a801fc3</passwdmd5>
                <purview type="purview">super</purview>
            </user>
        </adminuser>
    </administration>
</config>

 

用php修改xml的节点

$doc = new DOMDocument; 
$doc->load(‘test.xml‘);
$users = $doc->documentElement->getElementsByTagName(‘user‘);
foreach($users as $user) 
{
    $purviews = $user->getElementsByTagName(‘purview‘);
    $purview = $purviews->item(0);
     
    $tmpNode = $purview->cloneNode();
    $tmpNode->nodeValue = "xxx";   
    $tmpNode = $purview->parentNode->appendChild($tmpNode);
    $purview->parentNode->replaceChild($tmpNode,$purview);
    $doc->save("test1.xml"); 
} 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。