我需要使用用户提供的值更新xml节点;但我似乎无法修改simplexml对象。任何想法?
xml结构:
<xm>
<unit>
<building></building>
</unit>
<unit>
<building></building>
</unit>
<unit>
<building></building>
</unit>
<unit>
<building></building>
</unit>
</xm>
我试图这样做:
$original_xml = @simplexml_load_file("../test.xml");
$original_xml[$_POST['row_id']-1]->building = $_POST['building_txt'];
答案 0 :(得分:0)
做这样的事情:
$units = @simplexml_load_file("../test.xml");
$i = 1;
foreach($units->unit as $unit)
{
if($i == $_POST['row_id'])
{
$unit->building = $_POST['building_txt'];
}
$i++;
}
echo $units->asXML();
答案 1 :(得分:0)
以下工作(您需要引用unit
结构来指示您要检索的行:
$xml = simplexml_load_string('<xm>
<unit>
<building></building>
</unit>
<unit>
<building></building>
</unit>
<unit>
<building></building>
</unit>
<unit>
<building></building>
</unit>
</xm>');
$row = $_POST['row_id'] - 1;
$xml->unit[$row]->building = 'Test';
var_dump($xml->asXML());