如何使用php更新特定节点的XML元素?

如何使用php更新特定节点的XML元素?,php,xml,Php,Xml,你已经走到一半了。这样做: I tried this .. but it returns empty. $xmlFile = file_get_contents('floormap.xml'); //$xml = simplexml_load_string($xmlFile); $new= new SimpleXMLElement($xmlFile); $n=$new->xpath('//FLOOR1/B1OCK[@ID="F1"]'); var_dump($n); 如果现在查看$xml

你已经走到一半了。这样做:

I tried this .. but it returns empty.
$xmlFile = file_get_contents('floormap.xml');
//$xml = simplexml_load_string($xmlFile);
$new= new SimpleXMLElement($xmlFile);
$n=$new->xpath('//FLOOR1/B1OCK[@ID="F1"]');
var_dump($n);
如果现在查看
$xml
,则带有
ID='G7'
-节点的
名称
-属性将更改


查看它的工作情况:

展示您尝试过的方法,以及您尝试过但失败的方法。请更新您的问题。它应该做什么?解释:我需要使用另一个属性ID更新属性“name”。例如,通过一个查询字符串,我得到的ID值为F6,因此我必须将name属性更新为“ch1”,将URL更新为“gmail.com”
I tried this .. but it returns empty.
$xmlFile = file_get_contents('floormap.xml');
//$xml = simplexml_load_string($xmlFile);
$new= new SimpleXMLElement($xmlFile);
$n=$new->xpath('//FLOOR1/B1OCK[@ID="F1"]');
var_dump($n);
$xml = simplexml_load_string($x); // assume XML in $x
$update = $xml->xpath("//BlOCK[@ID='G7']")[0]; // requires PHP >= 5.4
$update['NAME'] = "Michi";