©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 5, PHP 7)
$data
)$newnode
)$exclusive
[, bool $with_comments
[, array $xpath
[, array $ns_prefixes
]]]] )$uri
[, bool $exclusive
[, bool $with_comments
[, array $xpath
[, array $ns_prefixes
]]]] )$deep
] )$newnode
[, DOMNode $refnode
] )$namespaceURI
)$node
)$feature
, string $version
)$prefix
)$namespaceURI
)$oldnode
)$newnode
, DOMNode $oldnode
)[#1] Ricki [2011-03-29 06:00:51]
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
[#2] matthijs at stdin dot nl [2011-03-29 03:06:26]
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
$doc = new DOMDocument();
$fragment = $doc->createDocumentFragment();
$fragment->appendChild($doc->createElement('foo'));
var_dump($fragment->firstChild);
$doc->appendChild($fragment);
var_dump($fragment->firstChild);
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) {
}