public function testAppendForeignElement() { $doc = new DOMDocument(); $dom = new \DOMDocument(); $foo = $doc->createElement('foo'); $doc->appendChild($foo); $bar = $dom->createElement('bar'); $baz = $dom->createElement('baz'); $doc->appendDomElement($bar, $foo); $expected = new \DOMDocument(); $expected->loadXML('<foo><bar></bar></foo>'); $this->assertEqualXMLStructure($expected->firstChild, $doc->firstChild); $expected = new \DOMDocument(); $expected->loadXML('<foo><bar></bar><baz></baz></foo>'); $foo->appendDomElement($baz); $this->assertEqualXMLStructure($expected->firstChild, $foo); }
/** * addCDATASection * * @param mixed $content string, SimpleXMLElement, or DOMDocument * @access public * @return void */ public function addCDATASection($content) { switch (true) { case is_string($content): break; case $content instanceof \SimpleXMLElement: $dom = dom_import_simplexml($content); $content = $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement); break; case $content instanceof \DOMDocument: $content = $content->saveXML($content->childNodes->item(0)); break; case $content instanceof \DOMNode: $dom = new DOMDocument(); $dom->appendChild($content); $content = $dom->saveXML($dom->childNodes->item(0)); break; default: throw new InvalidArgumentException('expected arguement 1 to be String, SimpleXMLElement, DOMNode, or DOMDocument, instead saw ' . gettype($content)); } $import = dom_import_simplexml($this); $node = $import->ownerDocument; $import->appendChild($node->createCDATASection($content)); }