修改和删除:
<?php
$doc=new DOMDocument();
$doc->load("php.xml");
$root=$doc->documentElement;
$books=$doc->getElementsByTagName("php100");
foreach ($books as $book) {
if( $book->getAttribute('id')==2){
echo $book->getAttribute('id')."-";
echo $book->getElementsByTagName("index")->item(0)->nodeValue='test';
echo "<br>";
}
if($book->getAttribute('id')==4){
$root->removeChild($book);
}
}
$doc->save('php.xml');
?>
PHP生成XML文件
<?php
$doc=new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput=true;
$php100=$doc->createElement_x('php100');
$php1000=$doc->createElement_x('php1000');
$php10000=$doc->createElement_x('title');
$php20000=$doc->createElement_x('content');
$val=$doc->createTextNode('333333333');
$val2=$doc->createTextNode('2222222222');
$val3=$doc->createTextNode('new');
$xmlnew=$doc->createAttribute('xmlnew');
$php20000->appendChild($val2);
$php10000->appendChild($val);
$php100->appendChild($php1000);
$php1000->appendChild($xmlnew);
$php1000->appendChild($php20000);
$php1000->appendChild($php10000);
$xmlnew->appendChild($val3);
$doc->appendChild($php100);
$doc->save('php.xml');
?>
<?xml version="1.0" encoding="UTF-8"?>
<php100>
<php1000 xmlnew="new">
<content>2222222222</content>
<title>333333333</title>
</php1000>
</php100>