Class SAXOutputter


  • public class SAXOutputter
    extends java.lang.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:
    DomTripSAXSource, DomTripXMLReader
    • Constructor Summary

      Constructors 
      Constructor Description
      SAXOutputter()
      Creates a new SAXOutputter with default settings.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isReportNamespaceDeclarations()
      Returns whether namespace declarations are reported as attributes.
      void output​(Document doc, org.xml.sax.ContentHandler handler)
      Emits SAX events for the given document to the specified content handler.
      void output​(Document doc, org.xml.sax.ContentHandler handler, org.xml.sax.ext.LexicalHandler lexicalHandler)
      Emits SAX events for the given document to the specified content and lexical handlers.
      void output​(Element element, org.xml.sax.ContentHandler handler)
      Emits SAX events for the given element subtree to the specified content handler.
      void output​(Element element, org.xml.sax.ContentHandler handler, org.xml.sax.ext.LexicalHandler lexicalHandler)
      Emits SAX events for the given element subtree to the specified handlers.
      void setReportNamespaceDeclarations​(boolean reportNamespaceDeclarations)
      Sets whether namespace declarations should be reported as attributes in startElement calls.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SAXOutputter

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

      • 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,
                           org.xml.sax.ContentHandler handler)
                    throws org.xml.sax.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:
        org.xml.sax.SAXException - if the content handler reports an error
        java.lang.IllegalArgumentException - if doc or handler is null
      • output

        public void output​(Document doc,
                           org.xml.sax.ContentHandler handler,
                           org.xml.sax.ext.LexicalHandler lexicalHandler)
                    throws org.xml.sax.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:
        org.xml.sax.SAXException - if a handler reports an error
        java.lang.IllegalArgumentException - if doc or handler is null
      • output

        public void output​(Element element,
                           org.xml.sax.ContentHandler handler)
                    throws org.xml.sax.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:
        org.xml.sax.SAXException - if the content handler reports an error
        java.lang.IllegalArgumentException - if element or handler is null
      • output

        public void output​(Element element,
                           org.xml.sax.ContentHandler handler,
                           org.xml.sax.ext.LexicalHandler lexicalHandler)
                    throws org.xml.sax.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:
        org.xml.sax.SAXException - if a handler reports an error
        java.lang.IllegalArgumentException - if element or handler is null