Class XMLStreamWriter
The XMLStreamWriter can be used to write small and large XML feeds.
Note: when this class is used with sensitive data, be careful in persisting sensitive information to disk.
The XMLStreamWriter does not perform well-formedness checking on its input. However the writeCharacters method escapes '&' , '<' and '>'. For attribute values the writeAttribute method escapes the above characters plus '"' to ensure that all character content and attribute values are well formed.
The following example illustrates how to use this class:
The code above will write the following to file:
Note: This output has been formatted for readability. See XMLIndentingStreamWriter.
| Property | Description |
|---|---|
| defaultNamespace: String | Returns the current default name space. |
| Constructor | Description |
|---|---|
| XMLStreamWriter(Writer) | Constructs the XMLStreamWriter for a writer. |
| Method | Description |
|---|---|
| close() | Close this writer and free any resources associated with the writer. |
| flush() | Write any cached data to the underlying output mechanism. |
| getDefaultNamespace() | Returns the current default name space. |
| getPrefix(String) | Gets the prefix the URI is bound to. |
| setDefaultNamespace(String) | Binds a URI to the default namespace. |
| setPrefix(String, String) | Sets the prefix the uri is bound to. |
| writeAttribute(String, String) | Writes an attribute to the output stream without a prefix. |
| writeAttribute(String, String, String) | Writes an attribute to the output stream. |
| writeAttribute(String, String, String, String) | Writes an attribute to the output stream. |
| writeCData(String) | Writes a CData section. |
| writeCharacters(String) | Write text to the output. |
| writeComment(String) | Writes an XML comment with the data enclosed. |
| writeDTD(String) | Write a DTD section. |
| writeDefaultNamespace(String) | Writes the default namespace to the stream. |
| writeEmptyElement(String) | Writes an empty element tag to the output. |
| writeEmptyElement(String, String) | Writes an empty element tag to the output. |
| writeEmptyElement(String, String, String) | Writes an empty element tag to the output. |
| writeEndDocument() | Closes any start tags and writes corresponding end tags. |
| 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. |
| writeEntityRef(String) | Writes an entity reference. |
| writeNamespace(String, String) | Writes a namespace to the output stream. |
| writeProcessingInstruction(String) | Writes a processing instruction. |
| writeProcessingInstruction(String, String) | Writes a processing instruction. |
| writeRaw(String) | Writes the given string directly into the output stream. |
| writeStartDocument() | Write the XML Declaration. |
| writeStartDocument(String) | Write the XML Declaration. |
| writeStartDocument(String, String) | Write the XML Declaration. |
| writeStartElement(String) | Writes a start tag to the output. |
| writeStartElement(String, String) | Writes a start tag to the output. |
| writeStartElement(String, String, String) | Writes a start tag to the output. |
assign, create, create, defineProperties, defineProperty, entries, freeze, fromEntries, getOwnPropertyDescriptor, getOwnPropertyNames, getOwnPropertySymbols, getPrototypeOf, hasOwnProperty, is, isExtensible, isFrozen, isPrototypeOf, isSealed, keys, preventExtensions, propertyIsEnumerable, seal, setPrototypeOf, toLocaleString, toString, valueOf, values
- defaultNamespace: String
Returns the current default name space.
- XMLStreamWriter(writer: Writer)
Constructs the XMLStreamWriter for a writer.
Parameters:
- writer - the writer for which the XMLStreamWriter is constructed.
- close(): void
Close this writer and free any resources associated with the writer. This method does not close the underlying writer.
- flush(): void
Write any cached data to the underlying output mechanism.
- getDefaultNamespace(): String
Returns the current default name space.
Returns:
- the current default name space.
- getPrefix(uri: String): String
Gets the prefix the URI is bound to.
Parameters:
- uri - the URI to use.
Returns:
- the prefix or null.
- setDefaultNamespace(uri: String): void
Binds a URI to the default namespace. This URI is bound in the scope of the current START_ELEMENT / END_ELEMENT pair. If this method is called before a START_ELEMENT has been written the uri is bound in the root scope.
Parameters:
- uri - the uri to bind to the default namespace, may be null.
- setPrefix(prefix: String, uri: String): void
Sets the prefix the uri is bound to. This prefix is bound in the scope of the current START_ELEMENT / END_ELEMENT pair. If this method is called before a START_ELEMENT has been written the prefix is bound in the root scope.
Parameters:
- prefix - the prefix to bind to the uri, may not be null.
- uri - the uri to bind to the prefix, may be null.
- writeAttribute(localName: String, value: String): void
Writes an attribute to the output stream without a prefix.
Parameters:
- localName - the local name of the attribute.
- value - the value of the attribute.
- writeAttribute(namespaceURI: String, localName: String, value: String): void
Writes an attribute to the output stream.
Parameters:
- namespaceURI - the uri of the prefix for this attribute.
- localName - the local name of the attribute.
- value - the value of the attribute.
- writeAttribute(prefix: String, namespaceURI: String, localName: String, value: String): void
Writes an attribute to the output stream.
Parameters:
- prefix - the prefix for this attribute.
- namespaceURI - the uri of the prefix for this attribute.
- localName - the local name of the attribute.
- value - the value of the attribute.
- writeCData(data: String): void
Writes a CData section.
Parameters:
- data - the data contained in the CData Section, may not be null.
- writeCharacters(text: String): void
Write text to the output.
Parameters:
- text - the value to write.
- writeComment(data: String): void
Writes an XML comment with the data enclosed.
Parameters:
- data - the data contained in the comment, may be null.
- writeDTD(dtd: String): void
Write a DTD section. This string represents the entire doctypedecl production from the XML 1.0 specification.
Parameters:
- dtd - the DTD to be written.
- writeDefaultNamespace(namespaceURI: String): void
Writes the default namespace to the stream.
Parameters:
- namespaceURI - the uri to bind the default namespace to.
- writeEmptyElement(localName: String): void
Writes an empty element tag to the output.
Parameters:
- localName - local name of the tag, may not be null.
- writeEmptyElement(namespaceURI: String, localName: String): void
Writes an empty element tag to the output.
Parameters:
- namespaceURI - the uri to bind the tag to, may not be null.
- localName - local name of the tag, may not be null.
- writeEmptyElement(prefix: String, localName: String, namespaceURI: String): void
Writes an empty element tag to the output.
Parameters:
- prefix - the prefix of the tag, may not be null.
- localName - local name of the tag, may not be null.
- namespaceURI - the uri to bind the tag to, may not be null.
- writeEndDocument(): void
Closes any start tags and writes corresponding end tags.
- writeEndElement(): void
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.
- writeEntityRef(name: String): void
Writes an entity reference.
Parameters:
- name - the name of the entity.
- writeNamespace(prefix: String, namespaceURI: String): void
Writes a namespace to the output stream. If the prefix argument to this method is the empty string, "xmlns", or null this method will delegate to writeDefaultNamespace.
Parameters:
- prefix - the prefix to bind this namespace to.
- namespaceURI - the uri to bind the prefix to.
- writeProcessingInstruction(target: String): void
Writes a processing instruction.
Parameters:
- target - the target of the processing instruction, may not be null.
- writeProcessingInstruction(target: String, data: String): void
Writes a processing instruction.
Parameters:
- target - the target of the processing instruction, may not be null.
- data - the data contained in the processing instruction, may not be null.
- writeRaw(raw: String): void
Writes the given string directly into the output stream. No checks regarding the correctness of the XML are done. The caller must ensure that the final result is a correct XML.
Parameters:
- raw - the string to write to the output stream.
- writeStartDocument(): void
Write the XML Declaration. Defaults the XML version to 1.0, and the encoding to utf-8
- writeStartDocument(version: String): void
Write the XML Declaration. Defaults the XML version to 1.0
Parameters:
- version - version of the xml document.
- writeStartDocument(encoding: String, version: String): void
Write the XML Declaration. Note that the encoding parameter does not set the actual encoding of the underlying output. That must be set when the instance of the XMLStreamWriter is created using the XMLOutputFactory.
Parameters:
- encoding - encoding of the xml declaration.
- version - version of the xml document.
- writeStartElement(localName: String): void
Writes a start tag to the output. All writeStartElement methods open a new scope in the internal namespace context. Writing the corresponding EndElement causes the scope to be closed.
Parameters:
- localName - local name of the tag, may not be null.
- writeStartElement(namespaceURI: String, localName: String): void
Writes a start tag to the output.
Parameters:
- namespaceURI - the namespaceURI of the prefix to use, may not be null.
- localName - local name of the tag, may not be null.
- writeStartElement(prefix: String, localName: String, namespaceURI: String): void
Writes a start tag to the output.
Parameters:
- prefix - the prefix of the tag, may not be null.
- localName - local name of the tag, may not be null.
- namespaceURI - the uri to bind the prefix to, may not be null.