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 DocumentFragment object.

  • Attach copies of the child nodes to the new object using the cloneNode( ) and appendChild( ) 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( );

Get XML in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.