- Simple API For XML Version 2 (SAX2)
- Auxiliary SAX Interfaces
- SAX and I/O
- SAX Error Handling
- The Glue of SAX: XMLReader
- The Document Object Model
- The Object Model
- The DOM and Factories
- The Node Interface
- Parents and Children
- Nonhierarchical Nodes
- Text Nodes
- Element and Attribute Nodes
- Document, Document Type, and Entity Nodes
- Bulk Insertion Using Document Fragment
- DOM Error Handling
- Implementation vs Interface
- DOM Traversal
- Where Are We?
Bulk Insertion Using Document Fragment
The DocumentFragment node is a very lightweight version of the Element object meant for DOM cut and paste operations. A DocumentFragment node is a generic container for other nodes that can be inserted as a group somewhere else in the hierarchy. The DocumentFragment node must implement the DocumentFragment interface, which is a marker interface that extends Node but adds no new methods. The key to the DocumentFragment node type is how other methods treat it when it is passed as a parameter. Consider the following Java code:
import org.w3c.dom.*; void dupEm(Document doc) { DocumentFragment frag = doc.createDocumentFragment(); NodeList authors = doc.getElementsByTagNameNS("urn:bru", "author"); frag.appendChild(authors.item(0)); frag.appendChild(authors.item(2)); doc.getDocumentElement().appendChild (frag); }
When frag is passed to appendChild, the implementation knows to "skip" the node being passed as an argument and instead enumerates its child nodes, appending each of them in turn. The following pseudocode shows how this is done:
void appendChild(Node node) { if (node.getNodeType() != Node.DOCUMENT_FRAGMENT_NODE) this.realAppendChild(node); else { for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { this.realAppendChild(child); } } }
Note that due to this special treatment, DocumentFragment nodes never appear in a document's parent/children hierarchy.