Class Serializer
The Serializer class is responsible for converting DomTrip's internal XML node tree back into XML text format. It provides intelligent formatting preservation that maintains the original formatting for unchanged content while applying new formatting rules to modified sections.
Serialization Features:
- Selective Preservation - Preserves formatting only for unmodified content
- Pretty Printing - Configurable indentation and formatting
- Raw Mode - Completely unformatted output (no line breaks or indentation)
- Automatic Detection - Intelligently detects and preserves existing formatting patterns
- Attribute Formatting - Preserves quote styles and attribute order
- Namespace Handling - Serialization of namespace declarations
Serialization Modes:
- Preservation Mode (
prettyPrint = false, default) - Maintains original formatting for unchanged content. Automatically detects formatting patterns in existing XML and preserves them when adding new content. - Pretty Print Mode (
prettyPrint = true) - Applies consistent indentation and formatting using configured settings. WhenlineEnding = ""andindentString = "", produces raw mode output.
Raw Mode:
Raw mode produces XML with no line breaks or indentation, resulting in a single
continuous line. This is useful for minimizing file size or when formatting is not desired.
Raw mode can be achieved by setting both lineEnding and indentString to
empty strings, or by using DomTripConfig.raw().
Automatic Formatting Detection:
When parsing existing XML documents, the system automatically detects the formatting style:
- Raw formatting - XML with no line breaks or custom spacing is detected and preserved when adding new content
- Pretty formatting - XML with consistent indentation is detected and preserved
- Custom formatting - XML with custom spacing patterns is detected and preserved
Usage Examples:
// Basic serialization with preservation (default)
Serializer serializer = new Serializer();
String xml = serializer.serialize(document);
// Pretty printing with custom indentation
Serializer prettySerializer = new Serializer();
prettySerializer.setPrettyPrint(true);
prettySerializer.setIndentString(" "); // 4 spaces
prettySerializer.setLineEnding("\n");
String prettyXml = prettySerializer.serialize(document);
// Raw mode (no formatting)
Serializer rawSerializer = new Serializer(DomTripConfig.raw());
String rawXml = rawSerializer.serialize(document);
// Result: <root><child>content</child></root>
// Manual raw mode configuration
Serializer manualRaw = new Serializer();
manualRaw.setPrettyPrint(true);
manualRaw.setIndentString(""); // No indentation
manualRaw.setLineEnding(""); // No line endings
// Serialize to OutputStream with encoding
OutputStream outputStream = new FileOutputStream("output.xml");
serializer.serialize(document, outputStream);
// Using configuration
DomTripConfig config = DomTripConfig.prettyPrint()
.withIndentString("\t")
.withLineEnding("\r\n");
Serializer configSerializer = new Serializer(config);
Performance Considerations:
The Serializer is optimized for performance:
- Uses StringBuilder for efficient string building
- Avoids re-serializing unmodified content when possible
- Provides both string and StringBuilder output methods
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new Serializer with default settings.Serializer(DomTripConfig config) Creates a new Serializer with the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptionGets the empty element style used for serialization.Deprecated.Deprecated.UseindentString()instead.Deprecated.UselineEnding()instead.Gets the indentation string used for pretty printing.booleanChecks if pretty printing is enabled.Gets the line ending string used for pretty printing.Serializes an XML document to string using this serializer's configuration.serialize(Document document, DomTripConfig config) Serializes an XML document to string with custom configuration.voidserialize(Document document, OutputStream outputStream) Serializes an XML document to an OutputStream using the document's encoding.voidserialize(Document document, OutputStream outputStream, String encoding) Serializes an XML document to an OutputStream using the specified encoding.voidserialize(Document document, OutputStream outputStream, Charset charset) Serializes an XML document to an OutputStream using the specified charset.Serializes a single node to string.voidserialize(Node node, OutputStream outputStream) Serializes a single node to an OutputStream using UTF-8 encoding.voidserialize(Node node, OutputStream outputStream, String encoding) Serializes a single node to an OutputStream using the specified encoding.voidserialize(Node node, OutputStream outputStream, Charset charset) Serializes a single node to an OutputStream using the specified charset.voidsetEmptyElementStyle(EmptyElementStyle emptyElementStyle) Sets the empty element style for serialization.voidsetIndentString(String indentString) Sets the indentation string for pretty printing.voidsetLineEnding(String lineEnding) Sets the line ending string for pretty printing.voidsetPrettyPrint(boolean prettyPrint) Sets whether to enable pretty printing.
-
Constructor Details
-
Serializer
public Serializer()Creates a new Serializer with default settings.Default settings include two-space indentation, pretty printing disabled (which preserves original formatting), and standard line endings.
-
Serializer
Creates a new Serializer with the specified configuration.- Parameters:
config- the DomTripConfig to use for serialization settings
-
-
Method Details
-
indentString
Gets the indentation string used for pretty printing.- Returns:
- the indentation string
-
getIndentString
Deprecated.UseindentString()instead.Gets the indentation string used for pretty printing.- Returns:
- the indentation string
-
setIndentString
Sets the indentation string for pretty printing.- Parameters:
indentString- the indentation string, or null for default
-
isPrettyPrint
public boolean isPrettyPrint()Checks if pretty printing is enabled.- Returns:
- true if pretty printing is enabled, false otherwise
-
setPrettyPrint
public void setPrettyPrint(boolean prettyPrint) Sets whether to enable pretty printing.- Parameters:
prettyPrint- true to enable pretty printing, false otherwise
-
lineEnding
Gets the line ending string used for pretty printing.- Returns:
- the line ending string
-
getLineEnding
Deprecated.UselineEnding()instead.Gets the line ending string used for pretty printing.- Returns:
- the line ending string
-
setLineEnding
Sets the line ending string for pretty printing.- Parameters:
lineEnding- the line ending string, or null for default
-
emptyElementStyle
Gets the empty element style used for serialization.- Returns:
- the empty element style
-
getEmptyElementStyle
Deprecated.UseemptyElementStyle()instead.Gets the empty element style used for serialization.- Returns:
- the empty element style
-
setEmptyElementStyle
Sets the empty element style for serialization.- Parameters:
emptyElementStyle- the empty element style, or null for default
-
serialize
Serializes an XML document to string with custom configuration.Creates a temporary serializer with the specified configuration and uses it to serialize the document. This allows one-time use of different serialization settings without modifying this serializer.
- Parameters:
document- the document to serializeconfig- the configuration to use for serialization- Returns:
- the serialized XML string, or empty string if document is null
-
serialize
Serializes an XML document to string using this serializer's configuration.If pretty printing is disabled and the document is unmodified, the original formatting will be preserved. Otherwise, the document will be serialized according to this serializer's configuration.
- Parameters:
document- the document to serialize- Returns:
- the serialized XML string, or empty string if document is null
-
serialize
Serializes an XML document to an OutputStream using the document's encoding.This method uses the document's encoding property to determine the character encoding for the output stream. If the document has no encoding specified, UTF-8 is used as the default.
- Parameters:
document- the document to serializeoutputStream- the OutputStream to write to- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Document document, OutputStream outputStream, Charset charset) throws DomTripException Serializes an XML document to an OutputStream using the specified charset.This method allows explicit control over the character encoding used for serialization, regardless of the document's encoding property.
- Parameters:
document- the document to serializeoutputStream- the OutputStream to write tocharset- the character encoding to use- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Document document, OutputStream outputStream, String encoding) throws DomTripException Serializes an XML document to an OutputStream using the specified encoding.This method allows explicit control over the character encoding used for serialization, regardless of the document's encoding property.
- Parameters:
document- the document to serializeoutputStream- the OutputStream to write toencoding- the character encoding name to use- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
Serializes a single node to an OutputStream using UTF-8 encoding.- Parameters:
node- the node to serializeoutputStream- the OutputStream to write to- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Node node, OutputStream outputStream, Charset charset) throws DomTripException Serializes a single node to an OutputStream using the specified charset.- Parameters:
node- the node to serializeoutputStream- the OutputStream to write tocharset- the character encoding to use- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
public void serialize(Node node, OutputStream outputStream, String encoding) throws DomTripException Serializes a single node to an OutputStream using the specified encoding.- Parameters:
node- the node to serializeoutputStream- the OutputStream to write toencoding- the character encoding name to use- Throws:
DomTripException- if serialization fails or I/O errors occur
-
serialize
Serializes a single node to string.If pretty printing is disabled and the node is unmodified, the original formatting will be preserved. Otherwise, the node will be serialized according to this serializer's configuration.
- Parameters:
node- the node to serialize- Returns:
- the serialized XML string, or empty string if node is null
-
emptyElementStyle()instead.