簡體   English   中英

PHP XML DOM未捕獲的異常“ DOMException”,消息為“錯誤的文檔錯誤”

[英]PHP XML DOM Uncaught exception 'DOMException' with message 'Wrong Document Error'

我正在嘗試學習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->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");


?>

任何幫助您解釋如何正確導入所有部門節點以能夠附加到它們的幫助將不勝感激。 我試過使用數組。

您需要導入任何節點以將其附加到另一個文檔:

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

我很確定這正在發生,因為您正在嘗試將其他文檔中的元素追加到輸出文檔中。

DOMNode::cloneNode php網站上的注釋中找到了此代碼這可能就是您想要的。

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

另外,您可以查看導出節點的XML並將其重新導入到DOMDocumentFragment ,但是我必須進行實驗才能確定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM