English 中文(简体)
PHP DOM - accessing newly added nodes
原标题:
  • 时间:2009-11-14 02:21:17
  •  标签:
  • php
  • dom

I use the following to get a html document into DOM:

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

and then I add some new content to an element in the html:

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

But if I now want to manipulate the #newbox, I can t do it because I can t access it with getElementById(). In order to do that I have to do the following (reloading with the new html):

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

Which works fine, but when having to do this between every dom manipulation, it becomes expensive performance-wise.

Is there any better way to "refresh" the DOM so that it works with the newly added elements?

Thanks in advance! :)

问题回答

On the save-and-load approach, you could also try Document.normalizeDocument. This should fix up the document as if it had been save-cycled, without actually really serialising. One thing that should do would be to re-calculate the isID-ness of attributes from the document type, which you d hope would have been set to one of the HTML doctypes (that define id as being an attribute of value type ID) by loadHTML.

(There is also Element.setIdAttribute which can be used to declare one instance of an Attr to contain an ID, but that s no use to you since you d have to get hold of it first.)

I haven t tested this though and it wouldn t surprise me if PHP didn t implement this DOM Level 3 Core stuff properly. By my interpretation of the spec for isId, I reckon it should have picked up the id type definition already automatically. (My own DOM implementation certainly does.) But in that case your code would have worked. And I suppose appendXML is a non-standard method after all, so there s nothing to say it has to resolve type definitions like loadXML or loadHTML would.

So, maybe a workaround is a better plan. You might use a DOMXPath to select the element by @id attribute rather than real IDness as such. Of course this will be much slower than getElementById, but hopefully faster than normalizeDocument.

Or just lose the XML string-slinging and stick to the DOM methods, if you can; then it s trivial to keep a reference to a created element. (You can use helper functions to create the elements a bit more quickly if you find the DOM methods too wordy for the amount of content you re creating.)

The only thing I know of that can handle that very well.. beautifuly is python s beautiful soup. The DOM is all split up into a parse tree which you can add to or take away at will mabey you can write a python script to handle the html and then coordinate the scripts by database or system call. alternatively server side javascript might be worth investigating.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签