Name
DocumentFragment
Synopsis
The DocumentFragment
is a lightweight container used to store XML
document fragments temporarily. Since it has no properties or
methods of its own, it can only provide the same functionality
exposed by the Node object. It
is intended to serve as a container for at least one well-balanced
XML subtree.
This object’s most obvious application is in the case of clipboard or drag-and-drop operations in a visual editor. The user may elect to select several subtrees that appear at the same level of the tree to be copied:
<document>
<parent>
<child_1></child_1>
<child_2></child_2>
</parent>
<parent>
</parent>
</document>If the user decides to copy the two child nodes to the clipboard, the DOM application would do the following:
Create a
DocumentFragmentobject.Attach copies of the child nodes to the new object using the
cloneNode( )andappendChild( )methods.
Then, when the user decides to paste the copied nodes to a
new location, the new DocumentFragment node is passed to this
target node’s appendChild( )
method. During the copy operation, the DocumentFragment node itself is ignored,
and only the children are attached to the target node.
Java example
// Create a Document Fragment object DocumentFragment dfNorm = doc.createDocumentFragment( );
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access