PHP XML DOM未捕获异常'DOMException',消息'Wrong Document Error'

Gra*_*ant 11 php xml dom xmldom

我正在尝试学习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>
Run Code Online (Sandbox Code Playgroud)

我知道SOT是我的根本元素,部门是SOT的"孩子",每个部门都有多个员工"孩子".我遇到的问题是当我试图将新员工添加到某个部门时.当我尝试时,$departmentArray->item($i)->appendChild($employee);我得到错误的文档错误.

我正在使用此PHP代码尝试将子项附加到departmentNode

<?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");


?>
Run Code Online (Sandbox Code Playgroud)

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

Ste*_*eAp 18

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

$departmentArray->item($i)->appendChild( $doc->importNode( $employee, true ) );
Run Code Online (Sandbox Code Playgroud)


Kev*_*eno 9

我很确定这种情况正在发生,因为您试图将来自不同文档的元素附加到输出文档中.

我发现这个代码中的注释上PHP的网站DOMNode::cloneNode,这可能是你想要的.

<?php 
    $dom1->documentElement->appendChild( 
        $dom1->importNode( $dom2->documentElement, true )
    ); 
?>
Run Code Online (Sandbox Code Playgroud)

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