Interface XMLWriter
- All Known Subinterfaces:
DOMWriter
- All Known Implementing Classes:
DOMWriterImpl,XMLStringWriter,XMLWriterBase,XMLWriterImpl,XMLWriterNSImpl,XMLWriterSAX
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.
- Version:
- 11 December 2011
-
Method Summary
Modifier and TypeMethodDescriptionvoidWrites an attribute.voidWrites an attribute.voidWrites an attribute.voidWrites an attribute.voidclose()Close the writer.voidClose the element automatically.voidOpens element, inserts text node and closes.voidemptyElement(String element) Writes an empty element.voidemptyElement(String uri, String element) Writes an empty element.voidflush()Flush the writer.voidopenElement(String name) Writes a start element tag correctly indented.voidopenElement(String name, boolean hasChildren) Writes a start element tag correctly indented.voidopenElement(String uri, String name, boolean hasChildren) Writes a start element tag correctly indented.voidsetIndentChars(String spaces) Sets the string to use for indentation.voidsetPrefixMapping(String uri, String prefix) Sets a prefix mapping.voidwriteCDATA(String data) Writes the given text as a CDATA section.voidwriteComment(String comment) Writes an XML comment.voidWrites an XML processing instruction.voidwriteText(char c) Writes the given character correctly for the encoding of this document.voidwriteText(char[] text, int off, int len) Write the given text correctly for the encoding of this document.voidWrites the given text correctly for the encoding of this document.voidwriteXML(char[] text, int off, int len) Write the given XML data.voidWrites the given XML data.voidxmlDecl()Writes the XML declaration.
-
Method Details
-
xmlDecl
Writes the XML declaration.Always:
<?xml version="1.0" encoding="encoding"?>
It is followed by a new line character if the indentation is turned on.
- Throws:
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.
-
setIndentChars
Sets the string to use for indentation.The string must be only composed of valid spaces characters.
If the string is
nullthen the indentation is turned off.- Parameters:
spaces- The indentation string to use.- Throws:
IllegalArgumentException- If the indent string is not made of spaces.IllegalStateException- If the writer has already been used.- See Also:
-
writeText
Writes the given character correctly for the encoding of this document.- Parameters:
c- The character to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeText
Writes the given text correctly for the encoding of this document.Does nothing if the text is
null.- Parameters:
text- The text to write- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeText
Write the given text correctly for the encoding of this document.- Parameters:
text- The text to write.off- The offset where we should start writing the string.len- The length of the character subarray to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeCDATA
Writes the given text as a CDATA section.Does nothing if the text is
null.- Parameters:
data- The data to write inside the CDATA section.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.IllegalArgumentException- If the implementation does not support CDATA nesting
-
writeXML
Writes the given XML data.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.- Parameters:
text- The text to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeXML
Write the given XML data.The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
- Parameters:
text- The text to write.off- The offset where we should start writing the string.len- The length of the character subarray to write.- Throws:
IOException- If an I/O exception is thrown by the underlying writer.
-
writeComment
Writes an XML comment.An XML comment is:
<!-- comment -->
Comments are not indented.
Does not write anything if the comment if
null.- Parameters:
comment- The comment to be written- Throws:
IOException- If thrown by the wrapped writer.IllegalArgumentException- If the comment contains "--".
-
writePI
Writes an XML processing instruction.An XML processing intruction is:
<?target data?>
- Parameters:
target- The PI's target.data- The PI's data.- Throws:
IOException- If an I/O exception occurs.
-
openElement
Writes a start element tag correctly indented.It is the same as
openElement(name, false)- Parameters:
name- the name of the element- Throws:
IOException- If an I/O exception occurs.- See Also:
-
openElement
Writes a start element tag correctly indented.Use the
hasChildrenparameter 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.
- Parameters:
name- The name of the elementhasChildren- true if this element has children- Throws:
IOException- If an I/O exception occurs.
-
openElement
Writes a start element tag correctly indented.Use the
hasChildrenparameter 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.
- Parameters:
uri- The namespace URI of the element.name- The name of the element.hasChildren- true if this element has children.- Throws:
IOException- If an I/O exception occurs.UnsupportedOperationException- If the implementing class does not handle namespace.
-
closeElement
Close the element automatically.The element is closed symmetrically to the
openElement(String, String, boolean)method if the XML writer is namespace aware or theopenElement(String, boolean)method.- Throws:
IOException- If an I/O exception occurs.
-
element
Opens element, inserts text node and closes.This method should behave like:
this.openElement(name, false); this.writeText(text); this.closeElement();
- Parameters:
name- The name of the element.text- The text of the element.- Throws:
IOException- If thrown by the wrapped writer.
-
emptyElement
Writes an empty element.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"/>- Parameters:
element- the name of the element- Throws:
IOException- If an I/O exception occurs.UnsupportedOperationException- If the implementing class does not handle namespace.
-
emptyElement
Writes an empty element.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"/>- Parameters:
uri- The namespace URI of the element.element- The name of the element.- Throws:
IOException- If an I/O exception occurs.UnsupportedOperationException- If the implementing class does not handle namespace.
-
attribute
Writes an attribute.- Parameters:
name- The name of the attribute.value- The value of the attribute.- Throws:
IOException- If thrown by the wrapped writer.IllegalStateException- If there is no open element or text has been written.
-
attribute
Writes an attribute.This method for number does not require escaping.
- Parameters:
name- The name of the attribute.value- The value of the attribute.- Throws:
IOException- If thrown by the wrapped writer.IllegalStateException- If there is no open element or text has been written.
-
attribute
Writes an attribute.- Parameters:
uri- The uri of the attribute.name- The name of the attribute.value- The value of the attribute.- Throws:
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.
-
attribute
Writes an attribute.This method for number does not require escaping.
- Parameters:
uri- The uri of the attribute.name- The name of the attribute.value- The value of the attribute.- Throws:
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.
-
setPrefixMapping
Sets a prefix mapping.- Parameters:
uri- The namespace URI.prefix- The new prefix for the namespace URI.- Throws:
UnsupportedOperationException- If the implementing class does not handle namespace.
-
flush
Flush the writer.- Throws:
IOException- If thrown by the wrapped writer.
-
close
Close the writer.- Throws:
IOException- If thrown by the wrapped writer.UnclosedElementException- If there is still an open element.
-