我正在使用DOMDocument来解析XML文件。我循环遍历不同的元素,看看是否有任何元素丢失,并使用错误消息填充具有createElement的数组。在结束我试图使用appendChild该数组,但我总是得到同样的错误信息:PHP:DOMNode :: appendChild元素数组
Uncaught exception 'DOMException' with message 'Wrong Document Error'
DOMNode->appendChild(Object(DOMElement))
1 {main}
thrown in /xxx/xxx.php on line 235
PHP Fatal error: Call to undefined method DOMElement::item() in /xxx/xxx.php on line 235.
代码如下:
$SMQuery = new DOMDocument();
$SMQuery->loadXML($params);
$response = $SMQuery->createElement('SMreply');
$errors = array();
if (!$reqtyp = $SMQuery->getElementsByTagName("tag1"))
{$errors[] = $SMQuery->createElement('error', 'tag1 Element is missing');}
if (!$reqtyp = $SMQuery->getElementsByTagName("tag2"))
{$errors[] = $SMQuery->createElement('error', 'tag2 Element is missing');}
......
if(!empty($errors))
{
foreach($errors as $error) {
$response->appendChild($error); <==== this line is causing the error !!!
}
}
任何帮助深表感谢。 干杯, Riki。
GOT IT !!!!我在原始代码中有一个错字! – 2012-02-02 16:31:01
那么,错字和纠正是什么? – 2017-08-04 13:37:49