//Write new data to DOM
$item = $dom->createElement("item");
$title = $dom->createElement("title");
$titleText = $dom-> ;createTextNode("title text");
$title->appendChild($titleText);
$item->appendChild($title);
$dom->documentElement->getElementsByTagName('channel')- >item(0)->appendChild($item);
//Remove node from DOM
//$dom->documentElement->RemoveChild($dom->documentElement->getElementsByTagName(" channel")->item(0));
//Or use xpath to query the node and delete it
//$dom->documentElement->RemoveChild($xpath->query("/rss/channel" )->item(0));
//$dom->save("newfile.xml");
//Modify node data from DOM
//Modify the first title file
// This place is clumsy. Create a new node and then replace the old node. If anyone has other good methods, please tell me
$firstTitle = $xpath->query("/rss/channel/item/title")->item(0);
$newTitle = $dom ->createElement("title");
$newTitle->appendChild(new DOMText("This's the new title text!!!"));
$firstTitle->parentNode->replaceChild($newTitle, $ firstTitle);
//Modify attributes
//$firstTitle = $xpath->query("/rss/channel/item/title")->item(0);
//$firstTitle->setAttribute( "orderby", "4");
$dom->save("newfile.xml");
echo "
以上就是在PHP5中使用DOM控制XML(2)的内容,更多相关文章请关注PHP中文网(www.php.cn)!