Class SAXOutputter

java.lang.Object
eu.maveniverse.domtrip.sax.SAXOutputter

public class SAXOutputter extends Object
Emits SAX ContentHandler events from a domtrip Document or Element tree.

This enables domtrip to participate in SAX-based XML processing pipelines such as XSLT processors, XML validators, serializers, and content pipelines. Rather than serializing to a string and re-parsing, the SAXOutputter walks the domtrip tree directly and emits the corresponding SAX events.

Note: Formatting preservation is intentionally lost at this boundary since SAX events do not carry formatting metadata. The value is interoperability, not round-tripping through SAX.

Usage Examples:

Document doc = Document.of(xml);

// Feed into a ContentHandler
SAXOutputter outputter = new SAXOutputter();
outputter.output(doc, contentHandler);

// With a LexicalHandler for comments and CDATA
outputter.output(doc, contentHandler, lexicalHandler);

// Output a single element subtree
outputter.output(element, contentHandler);

Namespace Handling:

The outputter emits startPrefixMapping/endPrefixMapping events for namespace declarations on each element. By default, namespace declaration attributes (xmlns, xmlns:prefix) are not included in the Attributes parameter of startElement. Set setReportNamespaceDeclarations(boolean) to true to include them, matching the SAX namespace-prefixes feature behavior.

Since:
1.3.0
See Also:
  • Constructor Details

    • SAXOutputter

      public SAXOutputter()
      Creates a new SAXOutputter with default settings.
  • Method Details

    • isReportNamespaceDeclarations

      public boolean isReportNamespaceDeclarations()
      Returns whether namespace declarations are reported as attributes.
      Returns:
      true if namespace declarations are included in startElement attributes
    • setReportNamespaceDeclarations

      public void setReportNamespaceDeclarations(boolean reportNamespaceDeclarations)
      Sets whether namespace declarations should be reported as attributes in startElement calls.

      When true, xmlns and xmlns:prefix attributes are included in the Attributes parameter, matching the SAX namespace-prefixes feature.

      Parameters:
      reportNamespaceDeclarations - true to include namespace declaration attributes
    • output

      public void output(Document doc, ContentHandler handler) throws SAXException
      Emits SAX events for the given document to the specified content handler.
      Parameters:
      doc - the document to output
      handler - the content handler to receive events
      Throws:
      SAXException - if the content handler reports an error
      IllegalArgumentException - if doc or handler is null
    • output

      public void output(Document doc, ContentHandler handler, LexicalHandler lexicalHandler) throws SAXException
      Emits SAX events for the given document to the specified content and lexical handlers.

      The lexical handler receives events for comments and CDATA sections. If null, comments and CDATA boundaries are silently skipped (CDATA text content is still emitted as regular characters).

      Parameters:
      doc - the document to output
      handler - the content handler to receive events
      lexicalHandler - the lexical handler for comments and CDATA, or null
      Throws:
      SAXException - if a handler reports an error
      IllegalArgumentException - if doc or handler is null
    • output

      public void output(Element element, ContentHandler handler) throws SAXException
      Emits SAX events for the given element subtree to the specified content handler.

      Unlike output(Document, ContentHandler), this method does not emit startDocument/endDocument events.

      Parameters:
      element - the element to output
      handler - the content handler to receive events
      Throws:
      SAXException - if the content handler reports an error
      IllegalArgumentException - if element or handler is null
    • output

      public void output(Element element, ContentHandler handler, LexicalHandler lexicalHandler) throws SAXException
      Emits SAX events for the given element subtree to the specified handlers.

      Unlike output(Document, ContentHandler, LexicalHandler), this method does not emit startDocument/endDocument events.

      Parameters:
      element - the element to output
      handler - the content handler to receive events
      lexicalHandler - the lexical handler for comments and CDATA, or null
      Throws:
      SAXException - if a handler reports an error
      IllegalArgumentException - if element or handler is null