错误文档 尝试向 xml 文档添加新节点时出错。使用 DOM 和 PHP

发布于 2024-11-26 09:08:28 字数 3047 浏览 1 评论 0原文

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

有什么帮助吗?

I want to create attribute and add it to xml element. I always get Wrong Document Error when i try to add any attribute or child elements to $commentElement. Error occurs in line marked with comment: // error. Here's my code:

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;
}

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素衣风尘叹 2024-12-03 09:08:28

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

您很可能只想要这个:

$parentElement->appendChild($atttribute);

Your new attribute is already a part of the current DOM object, even though it's not part of the DOM tree yet. importNode() is for importing nodes from OTHER DOM documents.

Most likely you'd simply want this:

$parentElement->appendChild($atttribute);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文