public interface XMLStreamWriter
This interface is similar to
javax.xml.stream.XMLStreamWriter; but it does not forces
dynamic allocation when formatting (any CharSequence
can be used instead of String).
Except for the speed (faster) and the added flexibility, the usage/behavior is about the same as its StAX counterpart.
This writer does not require creating new String objects
during XML formatting. Attributes values can be held by a single/reusable
TextBuilder
(or StringBuilder) instance to avoid adverse effects
on memory footprint (heap), garbage collection and performance.
// Creates a new writer (potentially recycled). XMLOutputFactory factory = OSGiServices.getXMLOutputFactory(); XMLStreamWriter writer = factory.createXMLStreamWriter(outputStream); TextBuilder tmp = new TextBuilder(); // To avoid creating new String instances. writer.writeStartDocument(); writer.writeStartElement("Time"); writer.writeAttribute("hour", tmp.clear().append(time.hour); writer.writeAttribute("minute", tmp.clear().append(time.minute); writer.writeAttribute("second", tmp.clear().append(time.second); writer.writeEndElement(); writer.writeStartDocument(); writer.close(); // Closes the writer (does not close underlying output stream).
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Close this writer and free any resources associated with the writer.
|
void |
flush()
Write any cached data to the underlying output mechanism.
|
CharSequence |
getPrefix(CharSequence uri)
Gets the prefix the specified uri is bound to.
|
Object |
getProperty(String name)
Gets the value of a feature/property from the underlying implementation.
|
void |
setDefaultNamespace(CharSequence uri)
Binds a URI to the default namespace.
|
void |
setPrefix(CharSequence prefix,
CharSequence uri)
Sets the prefix the uri is bound to.
|
void |
writeAttribute(CharSequence localName,
CharSequence value)
Writes an attribute to the output stream without a prefix.
|
void |
writeAttribute(CharSequence namespaceURI,
CharSequence localName,
CharSequence value)
Writes an attribute to the output stream.
|
void |
writeAttribute(CharSequence prefix,
CharSequence namespaceURI,
CharSequence localName,
CharSequence value)
Writes an attribute to the output stream.
|
void |
writeCData(CharSequence data)
Writes a CData section.
|
void |
writeCharacters(char[] text,
int start,
int length)
Writes text to the output.
|
void |
writeCharacters(CharSequence text)
Writes text to the output.
|
void |
writeComment(CharSequence data)
Writes an xml comment with the data enclosed.
|
void |
writeDefaultNamespace(CharSequence namespaceURI)
Writes the default namespace to the stream.
|
void |
writeDTD(CharSequence dtd)
Writes a DTD section (representing the entire doctypedecl
production from the XML 1.0 specification).
|
void |
writeEmptyElement(CharSequence localName)
Writes an empty element tag to the output.
|
void |
writeEmptyElement(CharSequence namespaceURI,
CharSequence localName)
Writes an empty element tag to the output.
|
void |
writeEmptyElement(CharSequence prefix,
CharSequence localName,
CharSequence namespaceURI)
Writes an empty element tag to the output.
|
void |
writeEndDocument()
Closes any start tags and writes corresponding end tags.
|
void |
writeEndElement()
Writes an end tag to the output relying on the internal state of the
writer to determine the prefix and local name of the event.
|
void |
writeEntityRef(CharSequence name)
Writes an entity reference
|
void |
writeNamespace(CharSequence prefix,
CharSequence namespaceURI)
Writes a namespace to the output stream.
|
void |
writeProcessingInstruction(CharSequence target)
Writes a processing instruction.
|
void |
writeProcessingInstruction(CharSequence target,
CharSequence data)
Writes a processing instruction
|
void |
writeStartDocument()
Writes the XML Declaration.
|
void |
writeStartDocument(CharSequence version)
Writes the XML Declaration.
|
void |
writeStartDocument(CharSequence encoding,
CharSequence version)
Writes the XML Declaration.
|
void |
writeStartElement(CharSequence localName)
Writes a start tag to the output.
|
void |
writeStartElement(CharSequence namespaceURI,
CharSequence localName)
Writes a start tag to the output.
|
void |
writeStartElement(CharSequence prefix,
CharSequence localName,
CharSequence namespaceURI)
Writes a start tag to the output.
|
void writeStartElement(CharSequence localName) throws XMLStreamException
localName - local name of the tag.XMLStreamExceptionvoid writeStartElement(CharSequence namespaceURI, CharSequence localName) throws XMLStreamException
namespaceURI - the namespaceURI of the prefix to use.localName - local name of the tag.XMLStreamException - if the namespace URI has not been bound
to a prefix and this writer does not repair namespaces.void writeStartElement(CharSequence prefix, CharSequence localName, CharSequence namespaceURI) throws XMLStreamException
localName - local name of the tag.prefix - the prefix of the tag.namespaceURI - the uri to bind the prefix to.XMLStreamException - if the namespace URI has not been bound
to a prefix and this writer does not repair namespaces.void writeEmptyElement(CharSequence namespaceURI, CharSequence localName) throws XMLStreamException
namespaceURI - the uri to bind the tag to.localName - local name of the tag.XMLStreamException - if the namespace URI has not been bound
to a prefix and this writer does not repair namespaces.void writeEmptyElement(CharSequence prefix, CharSequence localName, CharSequence namespaceURI) throws XMLStreamException
prefix - the prefix of the tag.localName - local name of the tag.namespaceURI - the uri to bind the tag to.XMLStreamException - if the namespace URI has not been bound
to a prefix and this writer does not repair namespaces.void writeEmptyElement(CharSequence localName) throws XMLStreamException
localName - local name of the tag.XMLStreamExceptionvoid writeEndElement()
throws XMLStreamException
XMLStreamExceptionvoid writeEndDocument()
throws XMLStreamException
XMLStreamExceptionvoid close()
throws XMLStreamException
XMLStreamExceptionvoid flush()
throws XMLStreamException
XMLStreamExceptionvoid writeAttribute(CharSequence localName, CharSequence value) throws XMLStreamException
localName - the local name of the attribute.value - the value of the attribute.IllegalStateException - if the current state does not allow
attribute writing.XMLStreamExceptionvoid writeAttribute(CharSequence prefix, CharSequence namespaceURI, CharSequence localName, CharSequence value) throws XMLStreamException
prefix - the prefix for this attribute.namespaceURI - the uri of the prefix for this attributelocalName - the local name of the attribute.value - the value of the attribute.IllegalStateException - if the current state does not allow
attribute writing.XMLStreamException - if the namespace URI has not been bound
to a prefix and this writer does not repair namespaces.void writeAttribute(CharSequence namespaceURI, CharSequence localName, CharSequence value) throws XMLStreamException
namespaceURI - the uri of the prefix for this attribute.localName - the local name of the attribute.value - the value of the attribute.IllegalStateException - if the current state does not allow
attribute writing.XMLStreamException - if the namespace URI has not been bound
to a prefix and this writer does not repair namespaces.void writeNamespace(CharSequence prefix, CharSequence namespaceURI) throws XMLStreamException
null this method
will delegate to writeDefaultNamespace.prefix - the prefix to bind this namespace to or nullnamespaceURI - the uri to bind the prefix.IllegalStateException - if the current state does not allow
namespace writing.XMLStreamExceptionvoid writeDefaultNamespace(CharSequence namespaceURI) throws XMLStreamException
namespaceURI - the uri to bind the default namespace to or
null (to map the prefix to "" URI)IllegalStateException - if the current state does not allow
namespace writing.XMLStreamExceptionvoid writeComment(CharSequence data) throws XMLStreamException
data - the data contained in the comment or nullXMLStreamExceptionvoid writeProcessingInstruction(CharSequence target) throws XMLStreamException
target - the target of the processing instruction.XMLStreamExceptionvoid writeProcessingInstruction(CharSequence target, CharSequence data) throws XMLStreamException
target - the target of the processing instruction.data - the data contained in the processing instruction.XMLStreamExceptionvoid writeCData(CharSequence data) throws XMLStreamException
data - the data contained in the CData Section.XMLStreamExceptionvoid writeDTD(CharSequence dtd) throws XMLStreamException
dtd - the DTD to be written.XMLStreamExceptionvoid writeEntityRef(CharSequence name) throws XMLStreamException
name - the name of the entity.XMLStreamExceptionvoid writeStartDocument()
throws XMLStreamException
XMLOutputFactory.XMLStreamExceptionvoid writeStartDocument(CharSequence version) throws XMLStreamException
XMLOutputFactory.version - the version of the xml document or null.XMLStreamExceptionvoid writeStartDocument(CharSequence encoding, CharSequence version) throws XMLStreamException
XMLOutputFactory.encoding - the encoding of the xml declaration or null.version - the version of the xml document or null.XMLStreamExceptionvoid writeCharacters(CharSequence text) throws XMLStreamException
text - the value to write or null.XMLStreamExceptionvoid writeCharacters(char[] text,
int start,
int length)
throws XMLStreamException
text - the value to writestart - the starting position in the array.length - the number of characters to write.XMLStreamExceptionCharSequence getPrefix(CharSequence uri) throws XMLStreamException
uri - namespace URInullXMLStreamExceptionvoid setPrefix(CharSequence prefix, CharSequence uri) throws XMLStreamException
prefix - the prefix to bind to the uri.uri - the uri to bind to the prefix or nullXMLStreamExceptionvoid setDefaultNamespace(CharSequence uri) throws XMLStreamException
uri - the uri to bind to the default namespace or null.XMLStreamExceptionObject getProperty(String name) throws IllegalArgumentException
name - the name of the property.IllegalArgumentException - if the property is not supported.Copyright © 2005-2013 Javolution. All Rights Reserved.