【问题标题】:PHP XML DOM Uncaught exception 'DOMException' with message 'Wrong Document Error'PHP XML DOM 未捕获异常 'DOMException' 并带有消息'错误文档错误'
【发布时间】:2023-03-05 10:59:01
【问题描述】:

我正在尝试学习 XML,我知道这是未正确导入节点的问题。但我无法完全弄清楚。我一直在环顾四周,大多数人没有像我在部门中那样拥有多个子元素。

这是我的 XML 结构:

<SOT>  
    <DEPARTMENT name="Aviation Technology" id="AT">  
        <EMPLOYEE type="Faculty">  
            <LOGIN>jdoe1</LOGIN>  
            <NAME>John Doe</NAME>   
        </EMPLOYEE>

        <EMPLOYEE type="Faculty">  
            <LOGIN>jdoe2</LOGIN>  
            <NAME>Jane Doe</NAME>   
        </EMPLOYEE>

        <EMPLOYEE type="Faculty">  
            <LOGIN>jdoe3</LOGIN>  
            <NAME>Joe Doe</NAME>  
        </EMPLOYEE> 
    </DEPARTMENT>    

    <DEPARTMENT name="Building and Construction Management" id="BCM">  
    </DEPARTMENT>

    <DEPARTMENT name="Computer Graphics Technology" id="CGT">  
    </DEPARTMENT>  
</SOT>

我了解 SOT 是我的根元素,部门是 SOT 的“子级”,每个部门都有多个员工“子级”。我遇到的问题是当我尝试将新员工添加到某个部门时。当我尝试$departmentArray-&gt;item($i)-&gt;appendChild($employee); 时,我得到了错误的文档错误。

我正在使用这个 PHP 代码尝试将孩子附加到部门节点

<?php

    //grab form data
    $username = $_POST['username'];
    $employeeName = $_POST['employeeName'];
    $department = $_POST['department'];

    //create new DOMDocument to hold current XML data
    $doc = new DOMDocument();
    $doc->load("test.xml");
    $xpath = new DOMXpath($doc);

    //create our new DOMDocument for combining the XML data
    $newDoc = new DOMDocument();
    $newDoc->preserveWhiteSpace = false;

    //create School of Tech Node and append to new doc
    $sotElement = $newDoc->createElement("SOT");
    $newDoc->appendChild($sotElement);
    $root = $newDoc->documentElement;

    //grab the department Nodes
    $departmentArray = $doc->getElementsByTagName("DEPARTMENT");

    //create a new employee and set attribute to faculty
    $employee = $newDoc->createElement("EMPLOYEE");
    $employee->setAttribute("type", "Faculty");

    //counters (might use them later for ->item(counter) function
    $indexCounter = 0;
    $i = 0;

    foreach($departmentArray as $departmentNode){
        if(strcmp($departmentNode->getAttribute('name'),$department) == 0){//check if departments match
            //create login element
            $loginNode = $newDoc->createElement("LOGIN");
            $loginNode->appendChild($newDoc->createTextNode($username));
            $employee->appendChild($loginNode);

            //create name node
            $nameNode = $newDoc->createElement("NAME");
            $nameNode->appendChild($newDoc->createTextNode($employeeName));
            $employee->appendChild($nameNode);

            //append employee onto department node
            //$departmentArray->item($i) = $doc->importNode($departmentArray->item($i), true);
            $departmentArray->item($i)->appendChild($employee);

            //set index of department array (possibly used for appending later)
            $indexCounter = $i;
        }
        $i++;
    }

    #######################################
    /*Write out data to XML file         */
    #######################################
    //$departmentArray = $doc->getElementsByTagName("DEPARTMENT");
    foreach($departmentArray as $departmentNode){
        $tempNode = $newDoc->importNode($departmentNode, true);
        /*if(strcmp($departmentNode->getAttribute('name'),$department) == 0){
            $sotElement->appendChild($employee);

        }*/
        $sotElement->appendChild($tempNode);
    }

    $newDoc->formatOutput = true;
    $newDoc->save("test2.xml");


?>

任何解释如何正确导入所有部门节点以便能够附加到它们的帮助将不胜感激。我试过使用数组。

【问题讨论】:

    标签: php xml dom xmldom


    【解决方案1】:

    您需要导入 any 节点以将其附加到 另一个 文档:

    $departmentArray->item($i)->appendChild( $doc->importNode( $employee, true ) );
    

    【讨论】:

    • 这是因为 departmentArray 当前位于原始 $doc 而不是 $newDoc 中吗?感谢您的帮助!
    • importNode的第二个参数很重要。默认为 false 且不导入子元素。
    【解决方案2】:

    我很确定这是因为您试图将不同文档中的元素附加到输出文档中。

    我在 php 的网站上为 DOMNode::cloneNode 找到了此代码 in a comment,这可能是您想要的。

    <?php 
        $dom1->documentElement->appendChild( 
            $dom1->importNode( $dom2->documentElement, true )
        ); 
    ?>
    

    或者,您可以查看导出节点的 XML 并将其重新导入到 DOMDocumentFragment,但我必须通过实验才能确定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2013-04-26
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      相关资源
      最近更新 更多