Class ProcessingInstruction
Processing instructions (PIs) provide a way to include application-specific
instructions in XML documents. They follow the syntax <?target data?>
where the target identifies the application and the data contains the instruction.
Processing Instruction Handling:
- Target Preservation - Maintains the PI target exactly as written
- Data Preservation - Preserves the instruction data
- Format Preservation - Keeps original formatting when unmodified
Common Processing Instructions:
<?xml version="1.0" encoding="UTF-8"?>- XML declaration<?xml-stylesheet type="text/xsl" href="style.xsl"?>- Stylesheet reference<?php echo "Hello World"; ?>- PHP code
Usage Examples:
// Create processing instruction
ProcessingInstruction pi = new ProcessingInstruction("xml-stylesheet",
"type=\"text/xsl\" href=\"style.xsl\"");
// Create using factory methods
ProcessingInstruction factoryPI = ProcessingInstruction.of("xml-stylesheet",
"type=\"text/css\" href=\"style.css\"");
ProcessingInstruction simplePI = ProcessingInstruction.of("target");
// Create and modify with fluent API
ProcessingInstruction fluentPI = ProcessingInstruction.of("target", "data")
.target("new-target")
.data("new data");
// Add to document
document.addChild(pi);
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class Node
Node.NodeType -
Field Summary
Fields inherited from class Node
modified, parent, precedingWhitespace -
Constructor Summary
ConstructorsConstructorDescriptionProcessingInstruction(String originalContent) ProcessingInstruction(String target, String data) -
Method Summary
Modifier and TypeMethodDescriptionaccept(DomTripVisitor visitor) Accepts a visitor for depth-first tree traversal.clone()Deprecated.copy()Creates a deep copy of this node.data()booleaninthashCode()static ProcessingInstructionCreates a processing instruction with only a target (no data).static ProcessingInstructionCreates a processing instruction with the specified target and data.voidoriginalContent(String originalContent) parent(ContainerNode parent) Sets the parent container node of this node.precedingWhitespace(String whitespace) Sets the whitespace that precedes this node.target()toString()voidtoXml(StringBuilder sb) Serializes this node to XML, appending to the provided StringBuilder.type()Returns the type of this XML node.Methods inherited from class Node
clearModified, depth, document, isDescendantOf, isModified, markModified, nextSibling, nextSiblingElement, parent, parentElement, precedingWhitespace, previousSibling, previousSiblingElement, siblingIndex, toXml
-
Constructor Details
-
ProcessingInstruction
-
ProcessingInstruction
-
-
Method Details
-
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.
-
precedingWhitespace
Sets the whitespace that precedes this node.This method allows control over the whitespace formatting before this node when serializing to XML.
- Overrides:
precedingWhitespacein classNode- Parameters:
whitespace- the whitespace string to set, null is treated as empty string- Returns:
- this processing instruction for method chaining
- See Also:
-
target
-
target
-
data
-
data
-
originalContent
-
originalContent
-
type
Description copied from class:NodeReturns 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.
- Specified by:
typein classNode- Returns:
- the
Node.NodeTypeof this node
-
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.
-
toXml
Description copied from class:NodeSerializes this node to XML, appending to the provided StringBuilder.This method is more efficient than
Node.toXml()when building larger XML documents as it avoids string concatenation overhead. -
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.
-
clone
Deprecated.Usecopy()instead.Creates a deep copy of this processing instruction. -
toString
-
equals
-
hashCode
-
of
Creates a processing instruction with the specified target and data.Factory method following modern Java naming conventions.
- Parameters:
target- the processing instruction targetdata- the processing instruction data- Returns:
- a new ProcessingInstruction
-
of
Creates a processing instruction with only a target (no data).Factory method for simple processing instructions.
- Parameters:
target- the processing instruction target- Returns:
- a new ProcessingInstruction with empty data
-
copy()instead.