0
好吧,我以前曾問過一個類似的問題,但它被拒絕了,因爲還有其他類似的問題,因此沒有收到答案。我仍然完全難住,所以我會很感激任何幫助。PHP中的XML removeChild使用ID
問題又拿出了之前在這裏:How do I remove a specific node using its attribute value in PHP XML Dom?
我已經適應我的代碼,在回答這個問題建議,它仍然無法正常工作。
我的XML是一個類似的佈局,以這樣的:
<fooBar>
<cat>
<thing id="111">
<title></title>
<desc></desc>
<link></link>
<img></img>
</thing>
<thing id="222">
<title></title>
<desc></desc>
<link></link>
<img></img>
</thing>
.............etc
</cat>
</fooBar>
我的PHP如下:
$r = $_GET['removeChild'];
$x = "upload/".($_GET['xml']);
$doc = new DOMDocument();
$doc->load($x);
$xpath = new DOMXpath($doc);
$nodeList = $xpath->query('//thing[@id="'.$r.'"]');
if ($nodeList->length) {
$node = $nodeList->item(0);
$node->parentNode->removeChild($node);
}
$doc->saveXML();
echo $x. "<br /> " .$r;
正確的價值觀正在呼應和XML文件被正確的加載我可以看到它已被修改,但沒有被刪除。我也嘗試將一個已知的id編碼到xpath行中,但仍得到相同的結果。我花了一天的最佳時間試圖讓這個工作,所以任何幫助都非常感謝。
我知道這是愚蠢的,但最好從別人那裏聽到它。謝謝! – tcnarss