PHP DOM - access to newly added nodes

I use the following to get the html document in the DOM:

$dom = new domDocument('1.0', 'utf-8');
$dom->loadHTML($html)

and then add new content to the element in html:

$element = $dom->getElementById('mybox');
$f = $dom->createDocumentFragment();
$f->appendXML('<div id="newbox">foo</div>');
$element->appendChild($f);

But if now I want to manipulate #newbox, I cannot do this because I cannot access it using getElementById(). To do this, I need to do the following (reboot with the new html):

$html = $dom->saveHTML();
$dom->loadHTML($html)

Which works well, but when you have to do this between each dom manipulator, it becomes expensive in performance.

Is there a better way to “update” the DOM so that it works with newly added elements?

Thanks in advance!:)

+3
source share
2 answers

Document.normalizeDocument. , , . , , - isID -ness , HTML ( id ) loadHTML.

( Element.setIdAttribute, Attr , , .)

, , PHP DOM Level 3 Core. isID, , id. ( DOM, , .) . , appendXML - , , , loadXML loadHTML.

, , - . DOMXPath, @id, . , , getElementById, , , , normalizeDocument.

XML- DOM, ; , . ( , , DOM .)

+1

, , .. beautifuly - python. DOM , , python script html, . javascript .

0

Source: https://habr.com/ru/post/1722836/


All Articles