Class XMLStringWriter
- All Implemented Interfaces:
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.
- Version:
- 7 March 2012
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringWriterWraps an XML Writerprivate final XMLWriterWraps an XML Writer -
Constructor Summary
ConstructorsConstructorDescriptionXMLStringWriter(boolean namespaces) Creates a new XML string writer.XMLStringWriter(boolean namespaces, boolean indent) Create a new XML string writer. -
Method Summary
Modifier and TypeMethodDescriptionvoidWrites an attribute.voidWrites an attribute.voidWrites an attribute.voidWrites an attribute.voidclose()Close the writer.voidClose the element automatically.private static voidDo nothingvoidOpens 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.toString()Returns the XML content as aString.voidwriteCDATA(String cdata) 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.
-
Field Details
-
writer
Wraps an XML Writer -
xml
Wraps an XML Writer
-
-
Constructor Details
-
XMLStringWriter
public XMLStringWriter(boolean namespaces) Creates a new XML string writer.
- Parameters:
namespaces- Whether this XML writer should use namespaces.
-
XMLStringWriter
public XMLStringWriter(boolean namespaces, boolean indent) Create a new XML string writer.
- Parameters:
namespaces- Whether this XML writer should use namespaces.indent- Set the indentation flag.
-
-
Method Details
-
xmlDecl
public void xmlDecl()Description copied from interface:XMLWriterWrites the XML declaration.Always:
<?xml version="1.0" encoding="encoding"?>
It is followed by a new line character if the indentation is turned on.
-
setIndentChars
Description copied from interface:XMLWriterSets 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.- Specified by:
setIndentCharsin interfaceXMLWriter- Parameters:
spaces- The indentation string to use.- See Also:
-
writeText
public void writeText(char c) Description copied from interface:XMLWriterWrites the given character correctly for the encoding of this document. -
writeText
Description copied from interface:XMLWriterWrites the given text correctly for the encoding of this document.Does nothing if the text is
null. -
writeText
public void writeText(char[] text, int off, int len) Description copied from interface:XMLWriterWrite the given text correctly for the encoding of this document. -
writeCDATA
Description copied from interface:XMLWriterWrites the given text as a CDATA section.Does nothing if the text is
null.- Specified by:
writeCDATAin interfaceXMLWriter- Parameters:
cdata- The data to write inside the CDATA section.
-
writeXML
Description copied from interface:XMLWriterWrites 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. -
writeXML
public void writeXML(char[] text, int off, int len) Description copied from interface:XMLWriterWrite 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.
-
writeComment
Description copied from interface:XMLWriterWrites an XML comment.An XML comment is:
<!-- comment -->
Comments are not indented.
Does not write anything if the comment if
null.- Specified by:
writeCommentin interfaceXMLWriter- Parameters:
comment- The comment to be written
-
writePI
Description copied from interface:XMLWriterWrites an XML processing instruction.An XML processing intruction is:
<?target data?>
-
openElement
Description copied from interface:XMLWriterWrites a start element tag correctly indented.It is the same as
openElement(name, false)- Specified by:
openElementin interfaceXMLWriter- Parameters:
name- the name of the element- See Also:
-
openElement
Description copied from interface:XMLWriterWrites 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.
- Specified by:
openElementin interfaceXMLWriter- Parameters:
name- The name of the elementhasChildren- true if this element has children
-
openElement
Description copied from interface:XMLWriterWrites 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.
- Specified by:
openElementin interfaceXMLWriter- Parameters:
uri- The namespace URI of the element.name- The name of the element.hasChildren- true if this element has children.
-
closeElement
public void closeElement()Description copied from interface:XMLWriterClose the element automatically.The element is closed symmetrically to the
XMLWriter.openElement(String, String, boolean)method if the XML writer is namespace aware or theXMLWriter.openElement(String, boolean)method.- Specified by:
closeElementin interfaceXMLWriter
-
element
Description copied from interface:XMLWriterOpens element, inserts text node and closes.This method should behave like:
this.openElement(name, false); this.writeText(text); this.closeElement();
-
emptyElement
Description copied from interface:XMLWriterWrites 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"/>- Specified by:
emptyElementin interfaceXMLWriter- Parameters:
element- the name of the element
-
emptyElement
Description copied from interface:XMLWriterWrites 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"/>- Specified by:
emptyElementin interfaceXMLWriter- Parameters:
uri- The namespace URI of the element.element- The name of the element.
-
attribute
Description copied from interface:XMLWriterWrites an attribute. -
attribute
Description copied from interface:XMLWriterWrites an attribute.This method for number does not require escaping.
-
attribute
Description copied from interface:XMLWriterWrites an attribute. -
attribute
Description copied from interface:XMLWriterWrites an attribute.This method for number does not require escaping.
-
setPrefixMapping
Description copied from interface:XMLWriterSets a prefix mapping.- Specified by:
setPrefixMappingin interfaceXMLWriter- Parameters:
uri- The namespace URI.prefix- The new prefix for the namespace URI.
-
flush
public void flush()Description copied from interface:XMLWriterFlush the writer. -
close
Description copied from interface:XMLWriterClose the writer.- Specified by:
closein interfaceXMLWriter- Throws:
UnclosedElementException- If there is still an open element.
-
toString
Returns the XML content as aString. -
doNothing
private static void doNothing()Do nothing
-