首页 > 代码库 > php 读写xml 修改xml

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 = "http://www.mamicode.com/xxx";       $tmpNode = $purview->parentNode->appendChild($tmpNode);    $purview->parentNode->replaceChild($tmpNode,$purview);    $doc->save("test1.xml"); } 

 

php 读写xml 修改xml