0
我浪費了大量時間試圖編輯和刪除xml文件中的特定節點。現在我成功刪除了該節點,但是當我嘗試編輯節點時,它只會更新最後一個節點。如何使用PHP編輯和刪除XML中的特定節點
我的XML文件如下:
<?xml version="1.0"?>
<reviews>
<product name="NokiaN9White">
<user name="testuser" id="02232012062451">
<username>[email protected]</username>
<date_post>23-02-2012</date_post>
<date_update>23-02-2012</date_update>
<review>Hello dear.</review>
</user>
<user name="testuser" id="02232012062521">
<username>[email protected]</username>
<date_post>23-02-2012</date_post>
<date_update>23-02-2012</date_update>
<review>Mrr</review>
</user>
<user name="testuser" id="02232012062523">
<username>[email protected]</username>
<date_post>23-02-2012</date_post>
<date_update>23-02-2012</date_update>
<review>adfasdf</review>
</user>
</product>
</reviews>
我來編輯這些數據的代碼是:
$xmlDoc = new DOMDocument();
$xmlDoc->load(dirname(__FILE__)."/reviews.xml");
$products = $xmlDoc->getElementsByTagName("product");
foreach($products as $product)
{
$prdoductN = $product->getAttribute("name");
if($prdoductN == $product_name){
$users = $product->getElementsByTagName("user");
$userR = get_userName($_SESSION['userName']);
foreach($users as $user){
$userName = $user->getAttribute("name");
$review_id = $user->getAttribute("id");
if($userName == $userR && $review_id == $r_id){
$current_date = date('d-m-Y');
$eventN = $user->getElementsByTagName("date_update");
$eventN->item(0)->nodeValue = $current_date;
$eventC = $user->getElementsByTagName("review");
$eventC->item(0)->nodeValue = $comments;
}
}
$xmlDoc->formatOutput = true;
//$xmlDoc->saveXML(); // This will return the XML as a string
$xmlDoc->save(dirname(__FILE__)."/reviews.xml");
}
}
謝謝你,但是這還需要安裝其他文件。(即:某些.DLL .SA)。我只需要通過只有PHP做到這一點。 – 2012-02-23 10:40:06