PHP XML DOM未捕获的异常'DOMException'与消息'错误的文档错误' [英] PHP XML DOM Uncaught exception 'DOMException' with message 'Wrong Document Error'

查看:117
本文介绍了PHP XML DOM未捕获的异常'DOMException'与消息'错误的文档错误'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习XML,我知道这是无法正确导入节点的问题。但我不太清楚。我一直在寻找,大多数人没有像部门这样的多个子元素。

I am trying to learn XML and I know this is a problem with not properly importing the Nodes. But I can't quite figure it out. I've been looking around and most people don't have multiple child elements like I do with the departments.

这是我的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); 我得到错误的文档错误。

I understand that SOT is my root element and that departments are "children" of SOT and each department has multiple employee "children". The problem I run into is when I am trying to add a new employee to a certain department. When I try $departmentArray->item($i)->appendChild($employee); I get the Wrong Document Error.

我正在使用这个PHP代码尝试将孩子附加到departmentNode

I'm using this PHP code to try and append the child to the 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");


?>

任何有助于解释如何正确导入所有部门节点以便附加到它们的帮助将是很大的赞赏。我尝试使用数组。

Any help explaining how to properly import all the department nodes to be able to append onto them would be greatly appreciated. I've tried using arrays.

推荐答案

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

You need to import any node to append it to another document:

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

这篇关于PHP XML DOM未捕获的异常'DOMException'与消息'错误的文档错误'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆