Class SAXOutputter
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns whether namespace declarations are reported as attributes.voidoutput(Document doc, ContentHandler handler) Emits SAX events for the given document to the specified content handler.voidoutput(Document doc, ContentHandler handler, LexicalHandler lexicalHandler) Emits SAX events for the given document to the specified content and lexical handlers.voidoutput(Element element, ContentHandler handler) Emits SAX events for the given element subtree to the specified content handler.voidoutput(Element element, ContentHandler handler, LexicalHandler lexicalHandler) Emits SAX events for the given element subtree to the specified handlers.voidsetReportNamespaceDeclarations(boolean reportNamespaceDeclarations) Sets whether namespace declarations should be reported as attributes instartElementcalls.
-
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:
trueif namespace declarations are included instartElementattributes
-
setReportNamespaceDeclarations
public void setReportNamespaceDeclarations(boolean reportNamespaceDeclarations) Sets whether namespace declarations should be reported as attributes instartElementcalls.When
true,xmlnsandxmlns:prefixattributes are included in theAttributesparameter, matching the SAXnamespace-prefixesfeature.- Parameters:
reportNamespaceDeclarations-trueto include namespace declaration attributes
-
output
Emits SAX events for the given document to the specified content handler.- Parameters:
doc- the document to outputhandler- the content handler to receive events- Throws:
SAXException- if the content handler reports an errorIllegalArgumentException- 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 outputhandler- the content handler to receive eventslexicalHandler- the lexical handler for comments and CDATA, or null- Throws:
SAXException- if a handler reports an errorIllegalArgumentException- if doc or handler is null
-
output
Emits SAX events for the given element subtree to the specified content handler.Unlike
output(Document, ContentHandler), this method does not emitstartDocument/endDocumentevents.- Parameters:
element- the element to outputhandler- the content handler to receive events- Throws:
SAXException- if the content handler reports an errorIllegalArgumentException- 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 emitstartDocument/endDocumentevents.- Parameters:
element- the element to outputhandler- the content handler to receive eventslexicalHandler- the lexical handler for comments and CDATA, or null- Throws:
SAXException- if a handler reports an errorIllegalArgumentException- if element or handler is null
-