PHP XML DOM 未捕获异常“DOMException”带有消息“错误文档错误”
我正在尝试学习 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 代码尝试将子级附加到部门节点。
<?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");
?>
如果有任何帮助解释如何正确导入所有部门节点以便能够附加到它们上,我们将不胜感激。我尝试过使用数组。
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.
Here is my XML structure:
<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>
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要导入任何节点才能将其附加到另一个文档:
You need to import any node to append it to another document:
我非常确定会发生这种情况,因为您正在尝试将不同文档中的元素附加到输出文档中。
我在 php 网站上 评论 中找到了这段代码
DOMNode::cloneNode
这可能就是你想要的。或者,您可以考虑导出节点的 XML 并将其重新导入到
DOMDocumentFragment
,但我必须尝试才能确定。I'm pretty sure that this is happening because you are trying to append an element from a different document into your output document.
I found this code in a comment on php's site for
DOMNode::cloneNode
which might be what you want.Alternatively, you could look at exporting the node's XML and reimporting it into a
DOMDocumentFragment
, but I'd have to experiment to know for sure.