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

13

我正在尝试学习XML,我知道这是没有正确导入节点的问题。但我无法完全弄清楚。我一直在寻找解决方法,但大多数人没有像我这样有多个子元素(如department)。

这是我的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->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");


?>
任何帮助解释如何正确导入所有部门节点以便能够附加到它们上面的内容将不胜感激。我已经尝试使用数组。

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

2个回答

23

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

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

这是因为departmentArray目前在原始$doc中而不是$newDoc中吗?感谢您的帮助! - Grant
importNode的第二个参数非常重要。默认情况下为false,不会导入子节点。 - jenkin90

15
我很确定这是因为您正在尝试将来自不同文档的元素附加到输出文档中。
我在php网站上找到了这段代码,在评论中,用于 DOMNode :: cloneNode ,可能是您想要的。
<?php 
    $dom1->documentElement->appendChild( 
        $dom1->importNode( $dom2->documentElement, true )
    ); 
?>

或者,您可以尝试将节点的XML导出并重新导入到DOMDocumentFragment中,但我需要进行实验才能确定。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接