public final class XMLStringWriter extends Object implements XMLWriter
This XML writer is backed by a StringWriter and will defer the XML writer's method to
either a XMLWriterImpl or XMLWriterNSImpl depending on whether namespace support is
required.
The write methods do not throw any IOException.
If the write is not set to support namespaces, the method which require a namespace URI will
throw an UnsupportedOperationException.
| Constructor and Description |
|---|
XMLStringWriter(boolean namespaces)
Creates a new XML string writer.
|
XMLStringWriter(boolean namespaces,
boolean indent)
Create a new XML string writer.
|
| 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.
|
String |
toString()
Returns the XML content as a
String. |
void |
writeCDATA(String cdata)
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.
|
public XMLStringWriter(boolean namespaces)
Creates a new XML string writer.
namespaces - Whether this XML writer should use namespaces.public XMLStringWriter(boolean namespaces,
boolean indent)
Create a new XML string writer.
namespaces - Whether this XML writer should use namespaces.indent - Set the indentation flag.public void xmlDecl()
XMLWriterAlways:
<?xml version="1.0" encoding="encoding"?>
It is followed by a new line character if the indentation is turned on.
public void setIndentChars(String spaces)
XMLWriterThe string must be only composed of valid spaces characters.
If the string is null then the indentation is turned off.
setIndentChars in interface XMLWriterspaces - The indentation string to use.Character.isSpaceChar(char)public void writeText(char c)
XMLWriterpublic void writeText(String text)
XMLWriterDoes nothing if the text is null.
public void writeText(char[] text,
int off,
int len)
XMLWriterpublic void writeCDATA(String cdata)
XMLWriterDoes nothing if the text is null.
writeCDATA in interface XMLWritercdata - The data to write inside the CDATA section.public void writeXML(String text)
XMLWriterThe 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.
public void writeXML(char[] text,
int off,
int len)
XMLWriterThe text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
public void writeComment(String comment)
XMLWriterAn XML comment is:
<!-- comment -->
Comments are not indented.
Does not write anything if the comment if null.
writeComment in interface XMLWritercomment - The comment to be writtenpublic void writePI(String target, String data)
XMLWriterAn XML processing intruction is:
<?target data?>
public void openElement(String name)
XMLWriterIt is the same as openElement(name, false)
openElement in interface XMLWritername - the name of the elementXMLWriter.openElement(java.lang.String, boolean)public void openElement(String name, boolean hasChildren)
XMLWriterUse 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.
openElement in interface XMLWritername - The name of the elementhasChildren - true if this element has childrenpublic void openElement(String uri, String name, boolean hasChildren)
XMLWriterUse 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.
openElement in interface XMLWriteruri - The namespace URI of the element.name - The name of the element.hasChildren - true if this element has children.public void closeElement()
XMLWriterThe element is closed symmetrically to the
XMLWriter.openElement(String, String, boolean) method if the XML writer is namespace
aware or the XMLWriter.openElement(String, boolean)method.
closeElement in interface XMLWriterpublic void element(String name, String text)
XMLWriterThis method should behave like:
this.openElement(name, false); this.writeText(text); this.closeElement();
public void emptyElement(String element)
XMLWriterIt 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"/>
emptyElement in interface XMLWriterelement - the name of the elementpublic void emptyElement(String uri, String element)
XMLWriterIt 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"/>
emptyElement in interface XMLWriteruri - The namespace URI of the element.element - The name of the element.public void attribute(String name, String value)
XMLWriterpublic void attribute(String name, int value)
XMLWriterThis method for number does not require escaping.
public void attribute(String uri, String name, String value)
XMLWriterpublic void attribute(String uri, String name, int value)
XMLWriterThis method for number does not require escaping.
public void setPrefixMapping(String uri, String prefix)
XMLWritersetPrefixMapping in interface XMLWriteruri - The namespace URI.prefix - The new prefix for the namespace URI.public void flush()
XMLWriterpublic void close()
throws UnclosedElementException
XMLWriterclose in interface XMLWriterUnclosedElementException - If there is still an open element.Copyright © 2007-2022. All Rights Reserved.