<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Import node into current document</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="domdocument.getelementsbytagnamens.html">DOMDocument::getElementsByTagNameNS</a></div> <div class="next" style="text-align: right; float: right;"><a href="domdocument.load.html">DOMDocument::load</a></div> <div class="up"><a href="class.domdocument.html">DOMDocument</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="domdocument.importnode" class="refentry"> <div class="refnamediv"> <h1 class="refname">DOMDocument::importNode</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">DOMDocument::importNode</span> — <span class="dc-title">Import node into current document</span></p> </div> <div class="refsect1 description" id="refsect1-domdocument.importnode-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.domnode.html" class="type DOMNode">DOMNode</a></span> <span class="methodname"><strong>DOMDocument::importNode</strong></span> ( <span class="methodparam"><span class="type"><a href="class.domnode.html" class="type DOMNode">DOMNode</a></span> <code class="parameter">$importedNode</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$deep</code></span> ] )</div> <p class="para rdfs-comment"> This function returns a copy of the node to import and associates it with the current document. </p> </div> <div class="refsect1 parameters" id="refsect1-domdocument.importnode-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">importedNode</code></em></span> <dd> <p class="para"> The node to import. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">deep</code></em></span> <dd> <p class="para"> If set to <strong><code>TRUE</code></strong>, this method will recursively import the subtree under the <em><code class="parameter">importedNode</code></em>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> To copy the nodes attributes <em><code class="parameter">deep</code></em> needs to be set to <strong><code>TRUE</code></strong> </p> </p></blockquote> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-domdocument.importnode-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The copied node or <strong><code>FALSE</code></strong>, if it cannot be copied. </p> </div> <div class="refsect1 errors" id="refsect1-domdocument.importnode-errors"> <h3 class="title">Errors/Exceptions</h3> <p class="para"> <a href="class.domexception.html" class="classname">DOMException</a> is thrown if node cannot be imported. </p> </div> <div class="refsect1 examples" id="refsect1-domdocument.importnode-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-5405"> <p><strong>Example #1 <span class="function"><strong>DOMDocument::importNode()</strong></span> example</strong></p> <div class="example-contents"><p> Copying nodes between documents. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$orgdoc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$orgdoc</span><span style="color: #007700">-></span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">"<root><element><child>text in child</child></element></root>"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// The node we want to import to a new document<br /></span><span style="color: #0000BB">$node </span><span style="color: #007700">= </span><span style="color: #0000BB">$orgdoc</span><span style="color: #007700">-></span><span style="color: #0000BB">getElementsByTagName</span><span style="color: #007700">(</span><span style="color: #DD0000">"element"</span><span style="color: #007700">)-></span><span style="color: #0000BB">item</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #FF8000">// Create a new document<br /></span><span style="color: #0000BB">$newdoc </span><span style="color: #007700">= new </span><span style="color: #0000BB">DOMDocument</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-></span><span style="color: #0000BB">formatOutput </span><span style="color: #007700">= </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Add some markup<br /></span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-></span><span style="color: #0000BB">loadXML</span><span style="color: #007700">(</span><span style="color: #DD0000">"<root><someelement>text in some element</someelement></root>"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"The 'new document' before copying nodes into it:\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-></span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// Import the node, and all its children, to the document<br /></span><span style="color: #0000BB">$node </span><span style="color: #007700">= </span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-></span><span style="color: #0000BB">importNode</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// And then append it to the "<root>" node<br /></span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-></span><span style="color: #0000BB">documentElement</span><span style="color: #007700">-></span><span style="color: #0000BB">appendChild</span><span style="color: #007700">(</span><span style="color: #0000BB">$node</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #DD0000">"\nThe 'new document' after copying the nodes into it:\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$newdoc</span><span style="color: #007700">-></span><span style="color: #0000BB">saveXML</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> The 'new document' before copying nodes into it: <?xml version="1.0"?> <root> <someelement>text in some element</someelement> </root> The 'new document' after copying the nodes into it: <?xml version="1.0"?> <root> <someelement>text in some element</someelement> <element> <child>text in child</child> </element> </root> </pre></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="domdocument.getelementsbytagnamens.html">DOMDocument::getElementsByTagNameNS</a></div> <div class="next" style="text-align: right; float: right;"><a href="domdocument.load.html">DOMDocument::load</a></div> <div class="up"><a href="class.domdocument.html">DOMDocument</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>