I’ve been trying to combine two XML documents like this:
$def = new DOMDocument( '1.0' );
$rdef = new DOMDocument( '1.0' );
$def->load( $path );
$rdef->loadXML( $info );
$r = $def->getElementsByTagName( 'repository' )->item( 0 );
$s = $rdef->getElementsByTagName( 'repository' )->item( 0 );
try {
$r->appendChild( $s );
}
catch ( DOMException $e ) {
SPConfig::debOut( get_class( $s ) );
SPConfig::debOut( $e->getMessage() );
}
The result is:
DOMElement
Wrong Document Error
So it seems to me that $rdef->getElementsByTagName( 'repository' )->item( 0 ) returns DOMElement object but the definition says it should be DOMNode object.
Any idea what is wrong about it?
Nothing wrong about it. DOMNodeList can hold any DOMNode instances or subclasses thereof. DOMElement extends DOMNode, so technically a DOMElement is a DOMNode as well. Same for DOMAttr.
EDIT: The problem is you trying to copy into the other DOMDocument. You have to
importNodethe node into the Document first, before appending it.EDIT2: Try this please:
EDIT3: Full example
gives