0

I am using DOMDocument to parse an XML file. I loop through the different Elements and see if any of them is missing and I fill an array with a createElement, with the error message. At the end I'm trying to appendChild that array but I always get the same error message:

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.

the code is as follow:

$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 !!!
 }
}

Any help is much appreciated. Cheers, Riki.

2
  • GOT IT!!!! I had a typo in my original code! Commented Feb 2, 2012 at 16:31
  • So what was the typo and the correction? Commented Aug 4, 2017 at 13:37

1 Answer 1

2

You don't show where $response is being defined, but if it's the result of another new DOMDocument(), then that explains you error - you can't add nodes from one DOM object to another directly. It has to be imported first via ->importNode(). Only after that can you actually append it.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry that line was missing. $response is defined (I just edited the code there and added it)!. ->importNode causes the same error message. I don't think I need to do an importNode, because if I do $test = $SMQuery->createElement('test','test'); $response->appendChild($test); that works fine, and do not need to use importNode!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.