可能重复:
如何使用 PHP 修改 xml 文件
我有一个看起来像这样的 xml:
<host host-name="www.1.com">
<root-directory>c:/public_html</root-directory>
</host>
<host host-name="www.2.com">
<root-directory>c:/public_html</root-directory>
</host>
我需要像这样使用php修改xml
如果主机名的值 = www.2.com
更改 c:/public_html/blahblah 中根目录的值
所以最终的结果是:
<host host-name="www.1.com">
<root-directory>c:/public_html</root-directory>
</host>
<host host-name="www.2.com">
<root-directory>c:/public_html/blahblah</root-directory>
</host>
我真的需要一些帮助。谢谢!
更新:
哦,我忘了说我不是程序员,反正这是我试过的
$xmlDomain= "www.2.com";
$nomefile='myfile.xml';
$xmlDoc = new DomDocument();
$xmlDoc->load($nomefile);
$Xroot = $xmlDoc->documentElement;
$products = $Xroot->getElementsByTagName("host");
$length = $products->length;
for ($i=$length-1;$i>=0;$i--)
{
$p = $products->item($i);
$pid = $p->getAttribute("host-name");
if ($pid == $xmlDomain)
{
$parent = $xmlDoc->getElementsByTagName("root-directory");
$parent->nodeValue = 'c:/public_html/blahblah';
}
}
$strxml = $xmlDoc->saveXML();
$handle = fopen($nomefile, "w");
fwrite($handle, $strxml);
fclose($handle);