【问题标题】:Wrong Document Error while trying to add new node to xml document. Using DOM and PHP尝试将新节点添加到 xml 文档时出现错误的文档错误。使用 DOM 和 PHP
【发布时间】:2011-07-25 15:51:13
【问题描述】:

我想创建属性并将其添加到 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;
}

有什么帮助吗?

【问题讨论】:

    标签: php xml dom


    【解决方案1】:

    您的新属性已经是当前 DOM 对象的一部分,即使它还不是 DOM 树的一部分。 importNode() 用于从 OTHER DOM 文档中导入节点。

    你很可能只是想要这个:

    $parentElement->appendChild($atttribute);
    

    【讨论】:

    • 我已经修改了我的代码以不使用 importNode 方法,但它仍然会产生与此注释相同的错误文档错误错误://error
    • 我已修改我的代码以不使用 importNode 方法,但它仍然产生相同的错误文档错误错误
    • 嗯。我猜这是因为您在加载外部 xml 文件之前创建了 $commentElement。完成加载后尝试创建它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-22
    • 2023-04-10
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多