Learning
Learning

Reputation: 868

how to get content of an element with HTML nodes?

I need to get the content of an element and place that content into another element. I use createTextNode to append that content as a child to the target element.

As I append it as text node, < and > is converted into &lt; and &gt;. How can I append that content without conversion?

For example:

<li id="fn1">
<div>
<a>some text
</a>
</div>
</li>

Expected output:

<p>
<div>
    <a>some text
    </a>
</div>
</p>

But my output is like,

<p>
&lt;div&gt;
&lt;a&gt;some text&lt;/a&gt;
&lt;/div&gt;
</p>

my code

$ch=dom->createElement("p");
$li=$xp->query("//li[contains(@id, 'fn')]");
  foreach($li as $liv) {
    $linodes = $liv->childNodes;
    $pvalue="";
       foreach ($linodes as $lin) {                                
       $pvalue.=$dom->saveXML($lin);}
$ch->appendChild($dom->createTextNode($pvalue)); }

I have tried, $ch->appendChild($dom->createTextNode(htmlspecialchars_decode($pvalue))); but same output

Upvotes: 2

Views: 104

Answers (1)

VolkerK
VolkerK

Reputation: 96189

If you want to

  • move a node within the same document: remove that node via DOMNode::removeChild and append the return value of that function via DOMNode::appendChild to its new parent node.
  • copy the node to a new location within the same document, make a deep clone of the node via DOMNode::clone the node and append it.
  • transfer the node to another document, import that node to the new document via DOMDOcument::importNode and then append it to its new parent.

Upvotes: 2

Related Questions