Class Node
- Direct Known Subclasses:
Comment, ContainerNode, ProcessingInstruction, Text
The Node class serves as the foundation for DomTrip's type-safe XML node hierarchy. It provides essential functionality for maintaining parent-child relationships, tracking modifications, and preserving whitespace formatting during round-trip parsing and serialization.
Node Hierarchy:
- Container Nodes -
DocumentandElementcan contain children - Leaf Nodes -
Text,Comment, andProcessingInstructioncannot contain children
Core Functionality:
- Whitespace Preservation - Maintains preceding and following whitespace
- Modification Tracking - Tracks changes for selective formatting preservation
- Parent-Child Relationships - Maintains bidirectional tree navigation
Usage Example:
// Access node properties
NodeType type = node.getNodeType();
ContainerNode parent = node.parent();
Element parentElement = node.getParentElement();
Document document = node.document();
int depth = node.getDepth();
// Check modification status
if (node.isModified()) {
// Node has been changed since parsing
}
// Serialize to XML
String xml = node.toXml();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEnumeration of XML node types supported by DomTrip. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanFlag indicating whether this node has been modified since parsingprotected ContainerNodeThe parent node of this node in the XML treeprotected StringWhitespace that appears before this node in the original XML -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract DomTripVisitor.Actionaccept(DomTripVisitor visitor) Accepts a visitor for depth-first tree traversal.voidclone()Deprecated.abstract Nodecopy()Creates a deep copy of this node.intdepth()Gets the depth of this node in the tree (root is 0).document()Gets the Document that contains this node.booleanisDescendantOf(Node ancestor) Checks if this node is a descendant of the given node.booleanvoidGets the next sibling node.Gets the next sibling that is an Element.parent()Gets the parent container node of this node.parent(ContainerNode parent) Sets the parent container node of this node.Gets the Element parent of this node.Gets the whitespace that precedes this node in the original XML.precedingWhitespace(String whitespace) Sets the whitespace that precedes this node.Gets the previous sibling node.Gets the previous sibling that is an Element.intGets the index of this node within its parent's children list.toXml()Serializes this node to an XML string.abstract voidtoXml(StringBuilder sb) Serializes this node to XML, appending to the provided StringBuilder.abstract Node.NodeTypetype()Returns the type of this XML node.
-
Field Details
-
parent
The parent node of this node in the XML tree -
precedingWhitespace
Whitespace that appears before this node in the original XML -
modified
protected boolean modifiedFlag indicating whether this node has been modified since parsing
-
-
Constructor Details
-
Node
protected Node()Creates a new XML node with default settings.Initializes the node with empty whitespace strings and sets the modification flag to false.
-
-
Method Details
-
type
Returns the type of this XML node.The node type determines the node's behavior and capabilities. This method must be implemented by all concrete node classes.
- Returns:
- the
Node.NodeTypeof this node
-
toXml
Serializes this node to an XML string.Creates a complete XML representation of this node and its children (if any), preserving original formatting for unmodified content.
- Returns:
- the XML string representation of this node
- See Also:
-
toXml
Serializes this node to XML, appending to the provided StringBuilder.This method is more efficient than
toXml()when building larger XML documents as it avoids string concatenation overhead.- Parameters:
sb- the StringBuilder to append the XML content to- See Also:
-
copy
Creates a deep copy of this node.The copied node will have:
- All properties copied from the original
- All child nodes recursively copied (for container nodes)
- Whitespace and formatting properties preserved
- No parent (parent is set to null)
The copied node and its descendants will have their parent-child relationships properly established within the copied subtree.
- Returns:
- a new node that is a deep copy of this node
- Since:
- 1.1.0
-
clone
Deprecated.Usecopy()instead.Creates a deep copy of this node. -
parent
Gets the parent container node of this node.Returns the parent container node in the XML tree, or null if this is the root node or if the node has not been added to a tree. Only Document and Element nodes can be parents since they are the only container nodes.
- Returns:
- the parent container node, or null if this node has no parent
- See Also:
-
parent
Sets the parent container node of this node.This method is typically called automatically when adding nodes to containers. Manual use should be done carefully to maintain tree consistency.
- Parameters:
parent- the parent container node to set, or null to clear the parent- Returns:
- this node for method chaining
- See Also:
-
precedingWhitespace
Gets the whitespace that precedes this node in the original XML.This includes any whitespace characters (spaces, tabs, newlines) that appeared before this node in the source XML. Preserving this whitespace enables lossless round-trip processing.
- Returns:
- the preceding whitespace string, never null
- See Also:
-
precedingWhitespace
Sets the whitespace that precedes this node.This method allows control over the whitespace formatting before this node when serializing to XML.
- Parameters:
whitespace- the whitespace string to set, null is treated as empty string- Returns:
- this node for method chaining
- See Also:
-
isModified
public boolean isModified() -
markModified
public void markModified() -
clearModified
public void clearModified() -
parentElement
Gets the Element parent of this node.Returns the parent if it's an Element, or null if the parent is a Document or if this node has no parent. Since parents can only be Element or Document, no traversal is needed.
- Returns:
- the Element parent, or null if parent is Document or no parent exists
- See Also:
-
document
Gets the Document that contains this node.Recursively traverses up the tree to find the root Document node. Every node in a properly constructed XML tree should have a Document as its ultimate parent.
- Returns:
- the Document containing this node, or null if not in a document tree
- See Also:
-
depth
public int depth()Gets the depth of this node in the tree (root is 0). -
isDescendantOf
Checks if this node is a descendant of the given node. -
siblingIndex
public int siblingIndex()Gets the index of this node within its parent's children list.- Returns:
- the index of this node, or -1 if this node has no parent
-
previousSibling
-
nextSibling
-
previousSiblingElement
-
accept
Accepts a visitor for depth-first tree traversal.This method implements the visitor pattern, allowing structured traversal of the XML tree with enter/exit lifecycle callbacks. Each node type dispatches to the appropriate visitor method.
- Parameters:
visitor- the visitor to accept- Returns:
- the action returned by the visitor, indicating how traversal should proceed
- Throws:
IllegalArgumentException- if visitor is null- Since:
- 1.3.0
- See Also:
-
nextSiblingElement
-
copy()instead.