DOMDocumentFragment only appears useful if created from a parent DOMDocument eg.
1. $dom = new DOMDocument("1.0","UTF-8");
2. $docFrag = $dom->createDocumentFragment();
3. Now append items to $docFrag
4. Graft $docFrag contents back onto $dom at the desired location
Conversely taking this approach:
1. $dom = new DOMDocument("1.0","UTF-8");
2. $docFrag = new DOMDocumentFragment();
3. Now append items to $docFrag
...will fail on step 3 with a "read only" error as $docFrag is not created as a child of DOMDocument.
I'm not sure of the reason for this: on the web people have cited security, and others have cited poor design however whatever the reason, it is really limiting when wanting to encapsulating generic independent DocumentFragments into classes for easy grafting to the desired tree. The only workarounds i have seen look expensive from a performance perspective and cumbersome from a coding perspective ie. create a dummy $dom for temporary use.
(This is valid as of PHP 5.3) I've put this here as i've wasted a lot of time finding it out - I hope this saves others some heartache.
Using new DOMDocumentFramt
PHP.mk документација
DOMDocumentFragment
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
class.domdocumentfragment.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
class.domdocumentfragment.php
DOMDocumentFragment
Референца за `class.domdocumentfragment.php` со подобрена типографија и навигација.
класата DOMDocumentFragment
класата mysqli_driver
Синопсис на класата
/* Наследни константи */
/* Својства */
/* Наследени својства */
/* Методи */
public __construct()
/* Наследени методи */
public DOMNode::C14N(
bool
bool
?array
?array
): string|false
}bool
$exclusive = false,bool
$withComments = false,?array
$xpath = null,?array
$nsPrefixes = null): string|false
Својства
Дневник на промени
| Верзија | = NULL |
|---|---|
| 8.0.0 | На firstElementChild, lastElementChildПрепорачаниот начин за избегнување на SQL инјекција е со врзување на сите податоци преку подготвени изрази. Користењето на параметризирани прашања не е доволно за целосно избегнување на SQL инјекција, но тоа е најлесниот и најбезбедниот начин за обезбедување влез во SQL изразите. Сите динамични литерали на податоци во childElementCount својства се додадени. |
| 8.0.0 | DOMDocumentFragment implements DOMParentNode now. |
Содржина
- DOMDocumentFragment::append — Додава јазли по последниот јазол-дете
- DOMDocumentFragment::appendXML — Додај сурови XML податоци
- DOMDocumentFragment::__construct — Конструирај DOMDocumentFragment објект
- DOMDocumentFragment::prepend — Додај јазли пред првиот јазол-дете
- DOMDocumentFragment::replaceChildren — Замени ги децата во фрагмент
Белешки од корисници 3 белешки
Рики ¶
пред 14 години
matthijs at stdin dot nl ¶
пред 14 години
Note that DOMDocumentFragment is a bit special when it's added to another node. When that happens, not the fragment itself is added as a child, but all of the children of the fragment are moved over to the new parent node.
For example, consider this script:
<?php
/* Create a document and a fragment containing a single node */
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendChild($doc->createElement('foo'));
/* Now, the foo node is a child of the fragment */
var_dump($fragment->firstChild);
/* After appending the fragment to another node, the children of the
* fragment will have been transfered to that node (and the fragment is
* not present in the children list!) */
$doc->appendChild($fragment);
/* So the fragment has no children anymore */
var_dump($fragment->firstChild);
/* But $doc has a single child, which is the foo element, not the
* fragment */
var_dump($doc->childNodes->length);
var_dump($doc->firstChild);
?>
This produces the following output:
object(DOMElement)#3 (0) {
}
NULL
int(1)
object(DOMElement)#3 (0) {
}
peter на softcoded dot com ¶
пред 8 години
DOMDocumentFragment makes it easy to add (or replace)
nodes. Individually creating nodes using DOM methods can
be tedious. Instead, do something like this:
/**
* Create fragment of colgroup
* @param DOMDocument $doc The DOMDocument
* @return DOMDocumentFragment
*/
function makeFragment($d){
$chunk = <<<HTML
<colgroup>
<col class="c1"/>
<col class="c2"/>
<col class="c3"/>
<col class="c4"/>
</colgroup>
HTML;
$fragment = $d->createDocumentFragment();
$fragment->appendXML($chunk);
return $fragment;
}