[英]Wrong Document Error while trying to add new node to xml document. Using DOM and PHP
我想創建屬性並將其添加到xml元素。 當我嘗試將任何屬性或子元素添加到$ commentElement時,總是出現錯誤的文檔錯誤。 在標有注釋的行中發生錯誤://錯誤。 這是我的代碼:
if (empty($_POST['userName']))
printErrorMessage("Username is not filled in");
if (empty($_POST['commentArea']))
printErrorMessage("Comment is not filled in");
$username = mysql_real_escape_string($_POST['userName']);
$comment = mysql_real_escape_string($_POST['commentArea']);
$newsId = $_GET['newsId'];
$fileContainingNew = $_GET['fileContainingNew'];
$xmlDoc = new DOMDocument('1.0', 'windows-1257');
$root = NULL;
$commentId = NULL;
$commentElement = $xmlDoc->createElement("komentaras");
if (!file_exists(substr($fileContainingNew, 0, strlen($fileContainingNew) - 4) . "Comments.xml")) {
$root = $xmlDoc->createElement("naujienuKomentarai");
$xmlDoc->appendChild($root);
$commentId = 1;
}
else {
$xmlDoc->load(substr($fileContainingNew, 0, strlen($fileContainingNew) - 4) . "Comments.xml");
$commentsList = $xmlDoc->getElementsByTagName("komentaras");
if ($commentsList->length == 0)
$commentId = 1;
else {
$biggestCommentId = 0;
foreach ($commentsList as $comment){
if ($newsId == $comment->getAttributeNode("newId")->value){
if ($comment->getAttributeNode("commentId")->value > $biggestCommentId)
$biggestCommentId = $comment->getAttributeNode("commentId")->value;
}
}
$commentId = $biggestCommentId++;
}
$root = $xmlDoc->documentElement;
}
$root->appendChild($commentElement);//error
$commentElement = appendAttribute($xmlDoc, $commentElement, "newId", $newsId);
$commentElement = appendAttribute($xmlDoc, $commentElement, "commentId", $commentId);
$commentElement = appendElement($xmlDoc, $commentElement, "autorius", $username);
$commentElement = appendElement($xmlDoc, $commentElement, "data", '20' . date("y-m-d"));
$commentElement = appendElement($xmlDoc, $commentElement, "laikas", "no val");
$commentElement = appendElement($xmlDoc, $commentElement, "ip", $_SERVER['REMOTE_ADDR']);
$commentElement = appendElement($xmlDoc, $commentElement, "komentaroTekstas", $comment);
$xmlDoc->formatOutput = true;
$xmlDoc->save(substr($fileContainingNew, 0, strlen($fileContainingNew) - 4) . "Comments.xml");
function appendAttribute($xmlDoc, $parentElement, $attributeName, $newAttributeValue){
$attribute = $xmlDoc->createAttribute($attributeName);
$attributeValue = $xmlDoc->createTextNode($newAttributeValue);
$parentElement->appendChild($attribute);
$attribute->appendChild($attributeValue);
return $parentElement;
}
function appendElement($xmlDoc, $parentElement, $newElementName, $newElementValue){
$newElement = $xmlDoc->createElement($newElementName);
$elementValue = $xmlDoc->createTextNode($newElementValue);
$newElement->appendChild($elementValue);
$parentElement->appendChild($newElement);
return $parentElement;
}
有什么幫助嗎?
您的新屬性已經是當前DOM對象的一部分,即使它還不是DOM樹的一部分。 importNode()用於從其他 DOM文檔中導入節點。
您最有可能只是想要這樣:
$parentElement->appendChild($atttribute);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.