1
<root>
<gallery name="First"/>
<gallery name="Second"/>
<gallery name="Third"/>
</root>
我試圖重命名多個 「名」 屬性立刻:用SimpleXML重命名多個屬性
$rename = array();
foreach($_POST['name'] as $value) {
$rename[] = $value;
}
$objXML = new SimpleXMLElement(XML_FILE_NAME, null, true);
$gallery = $objXML->xpath('/root/gallery/@name');
print_r($gallery);
print_r($rename);
$objXML->asXML(XML_FILE_NAME);
返回:
Array ([0] => SimpleXMLElement Object ([@attributes] => Array ([name] => First)) [1] => SimpleXMLElement Object ([@attributes] => Array ([name] => Second)) [2] => SimpleXMLElement Object ([@attributes] => Array ([name] => Third)))
Array ([0] => First New [1] => Second New [2] => Third New)
我怎樣才能讓PHP保存新值返回到XML?它需要另一個foreach循環嗎?代碼似乎已經變得太複雜了。
我想這一點,但沒有骰子:
foreach($objXML->xpath('/root/gallery/@name') as $gallery) {
$gallery = $_POST['name'];
}
我不看看'$ rename`在代碼 – stillstanding 2010-11-22 18:32:43