java.io.Closeable
, java.io.Flushable
, java.lang.AutoCloseable
public class XmlWriter
extends java.lang.Object
implements java.io.Flushable, java.io.Closeable
This class does not support all XML features. It supports specific features:
Pretty printing
Pretty printing may produce unexpected results with mixed content elements.
This implementation tries to produce data as soon as possible.
Discovering that an element has a mixed content may happen late, so that indentation
behavior changes after such discovery.
If one knows that an element has a mixed content, it is advised to indicate it to the
writer by writing an empty string content first in that element.
Modifier and Type | Class | Description |
---|---|---|
static class |
XmlWriter.Feature |
Enumeration of features supported by the writer.
|
Modifier and Type | Field | Description |
---|---|---|
static java.lang.String |
AMP |
|
static java.lang.String |
APOS |
|
protected static java.util.regex.Pattern |
CDATA_END_PATTERN |
|
static java.lang.String |
CR |
|
static java.lang.String |
GT |
|
static java.lang.String |
LF |
|
static java.lang.String |
LT |
|
static java.lang.String |
QUOT |
|
static java.lang.String |
TAB |
|
static java.lang.String |
XML_SCHEMA_INSTANCE |
|
static java.lang.String |
XMLNS |
|
static java.lang.String |
XMLNS_XSI |
|
static java.lang.String |
XSI_SCHEMA_LOCATION |
Constructor | Description |
---|---|
XmlWriter() |
Creates an XmlWriter to System.out, with UTF-8 encoding.
|
XmlWriter(java.io.File file) |
Creates an XmlWriter to a file using UTF-8 encoding.
|
XmlWriter(java.io.File file,
Compressor compressor) |
Creates an XmlWriter to a file with UTF-8 encoding and compressor.
|
XmlWriter(java.io.File file,
java.lang.String encoding) |
Creates an XmlWriter to a file with a given encoding.
|
XmlWriter(java.io.File file,
java.lang.String encoding,
Compressor compressor) |
Creates an XmlWriter to a file with a given encoding and compressor.
|
XmlWriter(java.io.OutputStream os) |
Creates an XmlWriter to an OutputStream with UTF-8 encoding.
|
XmlWriter(java.io.OutputStream os,
java.lang.String encoding) |
Creates an XmlWriter to an OutputStream with a given encoding.
|
XmlWriter(java.io.PrintStream out) |
Creates an XmlWriter to a PrintStream, with UTF-8 encoding.
|
XmlWriter(java.io.Writer writer) |
Creates an XmlWriter to a Writer, with UTF-8 encoding.
|
XmlWriter(java.io.Writer writer,
java.lang.String encoding) |
Creates an XmlWriter to a Writer with a given encoding.
|
XmlWriter(java.lang.String filename) |
Creates an XmlWriter to a file with UTF-8 encoding.
|
XmlWriter(java.lang.String filename,
Compressor compressor) |
Creates an XmlWriter to a file with a UTF-8 encoding and compressor.
|
XmlWriter(java.lang.String filename,
java.lang.String encoding) |
Creates an XmlWriter to a file with a given encoding.
|
XmlWriter(java.lang.String filename,
java.lang.String encoding,
Compressor compressor) |
Creates an XmlWriter to a file with a given encoding and compressor.
|
Modifier and Type | Method | Description |
---|---|---|
void |
addAttribute(java.lang.String name,
boolean value) |
Adds a boolean attribute to current element.
|
void |
addAttribute(java.lang.String name,
byte value) |
Adds a byte attribute to current element.
|
void |
addAttribute(java.lang.String name,
char value) |
Adds a char attribute to current element.
|
void |
addAttribute(java.lang.String name,
double value) |
Adds a double attribute to current element.
|
void |
addAttribute(java.lang.String name,
float value) |
Adds a float attribute to current element.
|
void |
addAttribute(java.lang.String name,
int value) |
Adds an int attribute to current element.
|
void |
addAttribute(java.lang.String name,
long value) |
Adds a long attribute to current element.
|
void |
addAttribute(java.lang.String name,
short value) |
Adds a short attribute to current element.
|
void |
addAttribute(java.lang.String name,
java.lang.Object value) |
Adds an Object attribute to current element.
|
void |
addAttribute(java.lang.String name,
java.lang.String value) |
Adds a String attribute to current element.
|
void |
addCData(java.lang.String content) |
Inserts a CData section with a content.
|
void |
addCDataContent(java.lang.String content) |
Adds content to current CData.
|
void |
addComment(java.lang.String content) |
Inserts a comment section with a content.
|
void |
addCommentContent(java.lang.String content) |
Adds content to current comment.
|
void |
addDefaultNamespace(java.lang.String uri) |
|
void |
addElement(java.lang.String name,
java.lang.String content) |
|
void |
addElementContent(java.lang.String content) |
|
void |
addElementIfNonEmpty(java.lang.String name,
java.lang.String content) |
|
void |
addNamespace(java.lang.String prefix,
java.lang.String uri) |
Adds a namespace declaration.
|
void |
addProcessingInstruction(java.lang.String target,
java.lang.String content) |
|
void |
addProcessingInstructionContent(java.lang.String content) |
|
void |
addStyleSheet(java.lang.String xsl) |
Helper function used to insert a StyleSheet Processing Instruction.
|
void |
beginCData() |
Starts the writing of a new CData.
|
void |
beginComment() |
Starts the writing of a new comment.
|
void |
beginDocument() |
Begins an XML document, using writer's encoding and version 1.0.
|
void |
beginDocument(java.lang.String version) |
Begins an XML document, using writer's encoding.
|
void |
beginElement(java.lang.String name) |
|
void |
beginProcessingInstruction(java.lang.String target) |
|
void |
close() |
|
protected static void |
dataError(java.lang.String message) |
|
void |
endCData() |
Closes the current CData.
|
void |
endComment() |
Closes the current comment.
|
void |
endDocument() |
|
void |
endElement() |
|
void |
endProcessingInstruction() |
|
void |
flush() |
|
CaseConverter |
getConverter() |
Returns the case converter.
|
java.lang.String |
getEndOfLine() |
|
java.lang.String |
getIndentString() |
|
boolean |
isEnabled(XmlWriter.Feature feature) |
Returns
true if a feature is enabled, false otherwise. |
void |
reset() |
|
void |
setCaseConverter(CaseConverter converter) |
|
XmlWriter |
setEnabled(XmlWriter.Feature... features) |
|
XmlWriter |
setEnabled(XmlWriter.Feature feature,
boolean enabled) |
Enables or disables a feature.
|
void |
setEndOfLine(java.lang.String eol) |
Set the string to use for end of lines.
|
XmlWriter |
setIndentString(java.lang.String s) |
Set the indent string.
|
void |
setTabSize(int tabSize) |
protected static final java.util.regex.Pattern CDATA_END_PATTERN
public static final java.lang.String LF
public static final java.lang.String CR
public static final java.lang.String TAB
public static final java.lang.String GT
public static final java.lang.String LT
public static final java.lang.String QUOT
public static final java.lang.String APOS
public static final java.lang.String AMP
public static final java.lang.String XMLNS
public static final java.lang.String XMLNS_XSI
public static final java.lang.String XML_SCHEMA_INSTANCE
public static final java.lang.String XSI_SCHEMA_LOCATION
public XmlWriter(java.io.Writer writer, java.lang.String encoding)
writer
- The writer.encoding
- The encoding to use.public XmlWriter(java.io.Writer writer)
writer
- The writer.public XmlWriter() throws java.io.IOException
java.io.UnsupportedEncodingException
- If UTF-8 is not supported, which should not happen.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.io.PrintStream out) throws java.io.IOException
out
- The PrintStream to use. Must be closed be the caller.java.io.UnsupportedEncodingException
- If UTF-8 encoding is not supported, which should not happen.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.io.OutputStream os, java.lang.String encoding) throws java.io.IOException
os
- The outputStream. Must be closed by caller.encoding
- The encoding to use.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.io.OutputStream os) throws java.io.IOException
os
- The outputStream. Must be closed by caller.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.lang.String filename, java.lang.String encoding, Compressor compressor) throws java.io.IOException
filename
- Name of the file.encoding
- The encoding to use.compressor
- The compressor.java.io.FileNotFoundException
- If the file exists but is a directory
rather than a regular file, does not exist but cannot be created,
or cannot be opened for any other reason.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.lang.String filename, java.lang.String encoding) throws java.io.IOException
filename
- Name of the file.encoding
- The encoding to use.java.io.FileNotFoundException
- If the file exists but is a directory
rather than a regular file, does not exist but cannot be created,
or cannot be opened for any other reason.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.lang.String filename, Compressor compressor) throws java.io.IOException
filename
- Name of the file.compressor
- The compressor.java.io.FileNotFoundException
- If the file exists but is a directory
rather than a regular file, does not exist but cannot be created,
or cannot be opened for any other reason.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.lang.String filename) throws java.io.IOException
filename
- Name of the file.java.io.FileNotFoundException
- If the file exists but is a directory
rather than a regular file, does not exist but cannot be created,
or cannot be opened for any other reason.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.io.File file, java.lang.String encoding, Compressor compressor) throws java.io.IOException
file
- File to use.encoding
- The encoding to use.compressor
- The compressor to use.java.io.FileNotFoundException
- If the file exists but is a directory
rather than a regular file, does not exist but cannot be created,
or cannot be opened for any other reason.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.io.File file, java.lang.String encoding) throws java.io.IOException
file
- File to use.encoding
- The encoding to use.java.io.FileNotFoundException
- If the file exists but is a directory
rather than a regular file, does not exist but cannot be created,
or cannot be opened for any other reason.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.io.File file, Compressor compressor) throws java.io.IOException
file
- File to use.compressor
- The compressor to use.java.io.FileNotFoundException
- If the file exists but is a directory
rather than a regular file, does not exist but cannot be created,
or cannot be opened for any other reason.java.io.UnsupportedEncodingException
- If the named encoding is not supported.java.io.IOException
- When an IO exception occurs.public XmlWriter(java.io.File file) throws java.io.IOException
file
- File to use.java.io.FileNotFoundException
- If the file exists but is a directory
rather than a regular file, does not exist but cannot be created,
or cannot be opened for any other reason.java.io.UnsupportedEncodingException
- If UTF-8 encoding is not supported, which should not happen.java.io.IOException
- When an IO exception occurs.protected static void dataError(java.lang.String message)
public final XmlWriter setEnabled(XmlWriter.Feature feature, boolean enabled)
feature
- The feature to enable/disable.enabled
- If true, enable the feature.public final XmlWriter setEnabled(XmlWriter.Feature... features)
public final boolean isEnabled(XmlWriter.Feature feature)
true
if a feature is enabled, false
otherwise.feature
- The feature.true
if feature
is enabled, false
otherwise.public final XmlWriter setIndentString(java.lang.String s)
s
- The indent string.public final java.lang.String getIndentString()
public final void setTabSize(int tabSize)
public final void setEndOfLine(java.lang.String eol)
This may be useful to force an OS specific symbol.
This has interactions with XmlWriter.Feature.USE_XML_EOL
.
WARNING: It is the responsibility of programmer to use a valid string.
eol
- The end of line string.public final java.lang.String getEndOfLine()
public final CaseConverter getConverter()
public final void setCaseConverter(CaseConverter converter)
public final void beginDocument(java.lang.String version) throws java.io.IOException
version
- The XML version to use.java.io.IOException
- If an I/O error occurs.public final void beginDocument() throws java.io.IOException
java.io.IOException
- If an I/O error occurs.public final void beginProcessingInstruction(java.lang.String target) throws java.io.IOException
java.io.IOException
public final void addProcessingInstructionContent(java.lang.String content) throws java.io.IOException
java.io.IOException
public final void endProcessingInstruction() throws java.io.IOException
java.io.IOException
public final void addProcessingInstruction(java.lang.String target, java.lang.String content) throws java.io.IOException
java.io.IOException
public final void addStyleSheet(java.lang.String xsl) throws java.io.IOException
xsl
- The URL of the style sheet to use.java.io.IOException
- If an I/O error occurs.public final void beginComment() throws java.io.IOException
java.io.IOException
- If an I/O error occurs.public final void addCommentContent(java.lang.String content)
content
- The text to add to current comment.public final void endComment() throws java.io.IOException
java.io.IOException
- If an I/O error occurs.public final void addComment(java.lang.String content) throws java.io.IOException
content
- The comment content.java.io.IOException
- If an I/O error occurs.public final void beginCData() throws java.io.IOException
java.io.IOException
- If an I/O error occurs.public final void addCDataContent(java.lang.String content)
content
- The text to add to current CData.public final void endCData() throws java.io.IOException
java.io.IOException
- If an I/O error occurs.public final void addCData(java.lang.String content) throws java.io.IOException
content
- The CData content.java.io.IOException
- If an I/O error occurs.public final void beginElement(java.lang.String name) throws java.io.IOException
java.io.IOException
public final void addAttribute(java.lang.String name, java.lang.Object value) throws java.io.IOException
If object is null adds an empty attribute.
Otherwise, uses toString() to convert value to a String.
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, java.lang.String value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, char value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, boolean value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, long value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, int value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, short value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, byte value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, double value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addAttribute(java.lang.String name, float value) throws java.io.IOException
name
- Attribute name.value
- Attribute value.java.io.IOException
- If an I/O error occurs.public final void addNamespace(java.lang.String prefix, java.lang.String uri) throws java.io.IOException
prefix
- Namespace prefixuri
- Associated URI.java.io.IOException
- If an I/O error occurs.public final void addDefaultNamespace(java.lang.String uri) throws java.io.IOException
java.io.IOException
public final void addElementContent(java.lang.String content) throws java.io.IOException
java.io.IOException
public final void endElement() throws java.io.IOException
java.io.IOException
public final void addElement(java.lang.String name, java.lang.String content) throws java.io.IOException
java.io.IOException
public final void addElementIfNonEmpty(java.lang.String name, java.lang.String content) throws java.io.IOException
java.io.IOException
public final void endDocument() throws java.io.IOException
java.io.IOException
public void flush() throws java.io.IOException
flush
in interface java.io.Flushable
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.lang.AutoCloseable
close
in interface java.io.Closeable
java.io.IOException
public void reset()
Copyright © 2019. All rights reserved.