0
<?xml version="1.0" encoding="UTF-8"?>
<FLOOR>
  <FLOOR1>
<BlOCK ID="F5" NAME="HallMark" URL="F1.COM"/>
<BlOCK ID="F6" NAME="F6" URL="F1.COM"/>
<BlOCK ID="F7" NAME="U.S. Polo Assn." URL="F1.COM"/>
  </FLOOR1>

    <FLOOR2>
<BlOCK ID="G4" NAME="Daiso" URL="G1.COM"/>
<BlOCK ID="G5" NAME="Lakhoos Exchange" URL="G1.COM"/>
<BlOCK ID="G6" NAME="4u" URL="G1.COM"/>
<BlOCK ID="G7" NAME="Aldo" URL="G1.COM"/>
<BlOCK ID="G8" NAME="Athlete's co." URL="G1.COM"/>

    </FLOOR2>
</FLOOR>

以上是我的 XML 文件。我想用 ID =G8 或说 ID=F7 更新“名称”的元素值。请帮忙。我尝试了很多代码。但失败了

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);
4

1 回答 1

0

你已经成功了一半。像这样做:

$xml = simplexml_load_string($x); // assume XML in $x
$update = $xml->xpath("//BlOCK[@ID='G7']")[0]; // requires PHP >= 5.4
$update['NAME'] = "Michi";

$xml如果你现在看, -nodeNAME的 -属性改变了。<BlOCK>ID='G7'

看到它工作:http ://codepad.viper-7.com/F9Pte1

于 2013-10-28T12:46:45.663 回答