Class ProcessingInstruction


  • public class ProcessingInstruction
    extends Node
    Represents an XML processing instruction, preserving exact formatting and content.

    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:
    Node, Document
    • Constructor Detail

      • ProcessingInstruction

        public ProcessingInstruction​(java.lang.String target,
                                     java.lang.String data)
      • ProcessingInstruction

        public ProcessingInstruction​(java.lang.String originalContent)
    • Method Detail

      • parent

        public ProcessingInstruction parent​(ContainerNode 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.

        Overrides:
        parent in class Node
        Parameters:
        parent - the parent container node to set, or null to clear the parent
        Returns:
        this processing instruction for method chaining
        See Also:
        Node.parent()
      • precedingWhitespace

        public ProcessingInstruction precedingWhitespace​(java.lang.String whitespace)
        Sets the whitespace that precedes this node.

        This method allows control over the whitespace formatting before this node when serializing to XML.

        Overrides:
        precedingWhitespace in class Node
        Parameters:
        whitespace - the whitespace string to set, null is treated as empty string
        Returns:
        this processing instruction for method chaining
        See Also:
        Node.precedingWhitespace()
      • target

        public java.lang.String target()
      • data

        public java.lang.String data()
      • originalContent

        public java.lang.String originalContent()
      • originalContent

        public void originalContent​(java.lang.String originalContent)
      • type

        public Node.NodeType type()
        Description copied from class: Node
        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.

        Specified by:
        type in class Node
        Returns:
        the Node.NodeType of this node
      • accept

        public DomTripVisitor.Action accept​(DomTripVisitor visitor)
        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.

        Specified by:
        accept in class Node
        Parameters:
        visitor - the visitor to accept
        Returns:
        the action returned by the visitor, indicating how traversal should proceed
        Since:
        1.3.0
        See Also:
        DomTripVisitor
      • toXml

        public void toXml​(java.lang.StringBuilder sb)
        Description copied from class: Node
        Serializes 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.

        Specified by:
        toXml in class Node
        Parameters:
        sb - the StringBuilder to append the XML content to
        See Also:
        Node.toXml()
      • copy

        public ProcessingInstruction 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.

        Specified by:
        copy in class Node
        Returns:
        a new node that is a deep copy of this node
        Since:
        1.1.0
      • clone

        @Deprecated
        public ProcessingInstruction clone()
        Deprecated.
        Use copy() instead.
        Creates a deep copy of this processing instruction.
        Overrides:
        clone in class Node
        Returns:
        a new processing instruction that is a copy of this processing instruction
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • of

        public static ProcessingInstruction of​(java.lang.String target,
                                               java.lang.String data)
        Creates a processing instruction with the specified target and data.

        Factory method following modern Java naming conventions.

        Parameters:
        target - the processing instruction target
        data - the processing instruction data
        Returns:
        a new ProcessingInstruction
      • of

        public static ProcessingInstruction of​(java.lang.String target)
        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