2011-04-25 72 views
11

我想學習XML,我知道這是一個問題,沒有正確導入節點。但我無法弄清楚。我一直在環顧四周,大多數人沒有像我對部門那樣的多個子元素。PHP的XML DOM未捕獲的異常'DOMException'與消息'錯誤的文檔錯誤'

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


?> 

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

回答

16

您需要導入任何節點到它的另一個文件附加到

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

這是因爲departmentArray目前是原來的$ doc和不$ newDoc裏面?謝謝您的幫助! – Grant 2011-04-26 00:15:06

+0

是的,這就是原因。 – SteAp 2011-04-26 18:28:18

+0

importNode的第二個參數很重要。默認情況下是false,不會導入孩子。 – jenkin90 2014-11-25 13:20:57

9

我很確定發生這種情況,因爲您試圖將來自不同文檔的元素附加到輸出文檔中。

我在php的網站上發現這個代碼in a comment對於DOMNode::cloneNode這可能是你想要的。

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

或者,你可以看看出口節點的XML,並將其重新導入到DOMDocumentFragment,但我不得不嘗試着去知道。

相關問題