2011-11-03 82 views
0

我需要用用戶提供的值更新xml節點;但我似乎無法修改simplexml對象。有任何想法嗎?如何在php 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

做這樣的事情:

$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(); 
0

下工作(需要引用unit s結構表示你想檢索:

$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());