public interface XMLWriter
This interface provides simple methods to write XML data onto a writer.
Most implementation should wrap a writer or an output stream. Implementations can be focused on performance, reliability, error reporting, etc...
For improved performance, the most efficient solution will generally to have an implementation write on a buffered writer since the memory usage will generally be restricted little more than the size of the buffer, and this will keep the I/O operation to a minimum.
Other implementations might want to wrap a SAX content handler.
Modifier and Type | Method and Description |
---|---|
void |
attribute(String name,
int value)
Writes an attribute.
|
void |
attribute(String name,
String value)
Writes an attribute.
|
void |
attribute(String uri,
String name,
int value)
Writes an attribute.
|
void |
attribute(String uri,
String name,
String value)
Writes an attribute.
|
void |
close()
Close the writer.
|
void |
closeElement()
Close the element automatically.
|
void |
element(String name,
String text)
Opens element, inserts text node and closes.
|
void |
emptyElement(String element)
Writes an empty element.
|
void |
emptyElement(String uri,
String element)
Writes an empty element.
|
void |
flush()
Flush the writer.
|
void |
openElement(String name)
Writes a start element tag correctly indented.
|
void |
openElement(String name,
boolean hasChildren)
Writes a start element tag correctly indented.
|
void |
openElement(String uri,
String name,
boolean hasChildren)
Writes a start element tag correctly indented.
|
void |
setIndentChars(String spaces)
Sets the string to use for indentation.
|
void |
setPrefixMapping(String uri,
String prefix)
Sets a prefix mapping.
|
void |
writeCDATA(String data)
Writes the given text as a CDATA section.
|
void |
writeComment(String comment)
Writes an XML comment.
|
void |
writePI(String target,
String data)
Writes an XML processing instruction.
|
void |
writeText(char c)
Writes the given character correctly for the encoding of this document.
|
void |
writeText(char[] text,
int off,
int len)
Write the given text correctly for the encoding of this document.
|
void |
writeText(String text)
Writes the given text correctly for the encoding of this document.
|
void |
writeXML(char[] text,
int off,
int len)
Write the given XML data.
|
void |
writeXML(String text)
Writes the given XML data.
|
void |
xmlDecl()
Writes the XML declaration.
|
void xmlDecl() throws IOException
Always:
<?xml version="1.0" encoding="encoding"?>
It is followed by a new line character if the indentation is turned on.
IOException
- If an I/O exception is thrown by the underlying writer.IllegalStateException
- If this method is called after the writer has started
writing elements nodes.void setIndentChars(String spaces)
The string must be only composed of valid spaces characters.
If the string is null
then the indentation is turned off.
spaces
- The indentation string to use.IllegalArgumentException
- If the indent string is not made of spaces.IllegalStateException
- If the writer has already been used.Character.isSpaceChar(char)
void writeText(char c) throws IOException
c
- The character to write.IOException
- If an I/O exception is thrown by the underlying writer.void writeText(String text) throws IOException
Does nothing if the text is null
.
text
- The text to writeIOException
- If an I/O exception is thrown by the underlying writer.void writeText(char[] text, int off, int len) throws IOException
text
- The text to write.off
- The offset where we should start writing the string.len
- The length of the character subarray to write.IOException
- If an I/O exception is thrown by the underlying writer.void writeCDATA(String data) throws IOException
Does nothing if the text is null
.
data
- The data to write inside the CDATA section.IOException
- If an I/O exception is thrown by the underlying writer.IllegalArgumentException
- If the implementation does not support CDATA nestingvoid writeXML(String text) throws IOException
The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
Does nothing if the text is null
.
text
- The text to write.IOException
- If an I/O exception is thrown by the underlying writer.void writeXML(char[] text, int off, int len) throws IOException
The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
text
- The text to write.off
- The offset where we should start writing the string.len
- The length of the character subarray to write.IOException
- If an I/O exception is thrown by the underlying writer.void writeComment(String comment) throws IOException
An XML comment is:
<!-- comment -->
Comments are not indented.
Does not write anything if the comment if null
.
comment
- The comment to be writtenIOException
- If thrown by the wrapped writer.IllegalArgumentException
- If the comment contains "--".void writePI(String target, String data) throws IOException
An XML processing intruction is:
<?target data?>
target
- The PI's target.data
- The PI's data.IOException
- If an I/O exception occurs.void openElement(String name) throws IOException
It is the same as openElement(name, false)
name
- the name of the elementIOException
- If an I/O exception occurs.openElement(java.lang.String, boolean)
void openElement(String name, boolean hasChildren) throws IOException
Use the hasChildren
parameter to specify whether this element is terminal
node or not, note: this affects the indenting. To produce correctly indented XML, you
should use the same value for this flag when closing the element.
The name can contain attributes and should be a valid xml name.
name
- The name of the elementhasChildren
- true if this element has childrenIOException
- If an I/O exception occurs.void openElement(String uri, String name, boolean hasChildren) throws IOException
Use the hasChildren
parameter to specify whether this element is terminal
node or not, note: this affects the indenting. To produce correctly indented XML, you
should use the same value for this flag when closing the element.
The name can contain attributes and should be a valid xml name.
uri
- The namespace URI of the element.name
- The name of the element.hasChildren
- true if this element has children.IOException
- If an I/O exception occurs.UnsupportedOperationException
- If the implementing class does not handle namespace.void closeElement() throws IOException
The element is closed symmetrically to the
openElement(String, String, boolean)
method if the XML writer is namespace
aware or the openElement(String, boolean)
method.
IOException
- If an I/O exception occurs.void element(String name, String text) throws IOException
This method should behave like:
this.openElement(name, false); this.writeText(text); this.closeElement();
name
- The name of the element.text
- The text of the element.IOException
- If thrown by the wrapped writer.void emptyElement(String element) throws IOException
It is possible for the element to contain attributes, however, since there is no character escaping, great care must be taken not to introduce invalid characters. For example:
<example test="yes"/>
element
- the name of the elementIOException
- If an I/O exception occurs.UnsupportedOperationException
- If the implementing class does not handle namespace.void emptyElement(String uri, String element) throws IOException
It is possible for the element to contain attributes, however, since there is no character escaping, great care must be taken not to introduce invalid characters. For example:
<example test="yes"/>
uri
- The namespace URI of the element.element
- The name of the element.IOException
- If an I/O exception occurs.UnsupportedOperationException
- If the implementing class does not handle namespace.void attribute(String name, String value) throws IOException
name
- The name of the attribute.value
- The value of the attribute.IOException
- If thrown by the wrapped writer.IllegalStateException
- If there is no open element or text has been written.void attribute(String name, int value) throws IOException
This method for number does not require escaping.
name
- The name of the attribute.value
- The value of the attribute.IOException
- If thrown by the wrapped writer.IllegalStateException
- If there is no open element or text has been written.void attribute(String uri, String name, String value) throws IOException
uri
- The uri of the attribute.name
- The name of the attribute.value
- The value of the attribute.IOException
- If thrown by the wrapped writer.IllegalStateException
- If there is no open element or text has been written.UnsupportedOperationException
- If the implementing class does not handle namespace.void attribute(String uri, String name, int value) throws IOException
This method for number does not require escaping.
uri
- The uri of the attribute.name
- The name of the attribute.value
- The value of the attribute.IOException
- If thrown by the wrapped writer.IllegalStateException
- If there is no open element or text has been written.UnsupportedOperationException
- If the implementing class does not handle namespace.void setPrefixMapping(String uri, String prefix)
uri
- The namespace URI.prefix
- The new prefix for the namespace URI.UnsupportedOperationException
- If the implementing class does not handle namespace.void flush() throws IOException
IOException
- If thrown by the wrapped writer.void close() throws IOException
IOException
- If thrown by the wrapped writer.UnclosedElementException
- If there is still an open element.Copyright © 2007-2014. All Rights Reserved.