public class XmlWriter
extends java.lang.Object
The XmlWriter class exposes a number of protected methods that enable it
to be subclassed for the purposes of customizing its output. See
com.google.javascript.util.JsonWriter
for an example.
Set<XmlWriter.WriterFlags>
to the constructor:
new XmlWriter(sw, EnumSet.of(WriterFlags.WRITE_HEADER, WriterFlags.EXPAND_EMPTY, WriterFlags.PRETTY_PRINT), null)The caller can supply any of the values enumerated in
XmlWriter.WriterFlags
, or none.
Once a feature has been enabled in the constructor, it cannot be turned off.
WRITE_HEADER
flags causes XmlWriter
to emit an XML
header at the beginning of the XML document:
<?xml version='1.0'?>
EXPAND_EMPTY
flags causes XmlWriter
to emit "expanded"
empty elements (elements consisting of distinct begin and end tags):
<foo> <wee really="yeah"></wee> </foo>
The PRETTY_PRINT
flag enables pretty printing. This feature
formats the XML output with using new lines and tab characters:
<foo> <bar> <wee really="yeah"/> </bar> </foo>
Will produce wonky formatting:
w.startElement(null, "txt", null, null); w.simpleElement(null, "fooey", null, null); w.characters("Kleenex"); w.endElement(null, "txt");
<txt> <fooey/>Kleenex </txt>
You can ensure correct formatting of mixed content in your document
by using the innerXml(String)
method to write raw XML.
Correctly formatted:
w.startElement(null, "txt", null, null); w.innerXml("<fooey/>"); w.characters("Kleenex"); w.endElement(null, "txt");
<txt><fooey/>Kleenex</txt>
Modifier and Type | Class and Description |
---|---|
static class |
XmlWriter.Attribute
The Attribute class represents an XML attribute.
|
static class |
XmlWriter.Namespace
Deprecated.
Use the
XmlNamespace class instead. |
static class |
XmlWriter.WriterFlags
Enumeration type that can be used to configure the XmlWriter behavior.
|
Constructor and Description |
---|
XmlWriter(java.io.Writer w)
Constructs an XmlWriter instance associated that will generate
XML content to an underlying
Writer . |
XmlWriter(java.io.Writer w,
boolean includeHeader)
Deprecated.
|
XmlWriter(java.io.Writer w,
java.util.Set<XmlWriter.WriterFlags> f,
java.lang.String encoding)
The default namespace that will take effect on the next
element transition.
|
XmlWriter(java.io.Writer w,
java.util.Set<XmlWriter.WriterFlags> f,
java.lang.String encoding,
boolean standalone)
Constructor that allows standalone directive to be provided.
|
XmlWriter(java.io.Writer w,
java.lang.String encoding)
Constructor that writers header including encoding information.
|
Modifier and Type | Method and Description |
---|---|
void |
characters(java.lang.String s)
Emits character data subject to XML escaping.
|
void |
characters(java.lang.String s,
boolean useCData)
Emits character data subject to either XML escaping or CDATA escaping.
|
void |
close()
Closes the XmlWriter and the underlying output writer.
|
void |
endElement()
Ends the current element.
|
void |
endElement(XmlNamespace namespace,
java.lang.String name)
Ends the current element.
|
void |
endRepeatingElement()
Indicates that the series of repeating elements have been completely
written.
|
void |
flush()
Flushes the XmlWriter and the underlying output writer.
|
void |
innerXml(java.lang.String xml)
Writes inner XML provided as a string.
|
void |
setDefaultNamespace(XmlNamespace namespace)
Sets the default namespace.
|
void |
simpleElement(java.lang.String name,
java.lang.String value)
Emits a simple element (without child elements).
|
void |
simpleElement(XmlNamespace namespace,
java.lang.String name,
java.util.List<XmlWriter.Attribute> attrs,
java.lang.String value)
Emits a simple element (without child elements).
|
void |
startElement(java.lang.String name)
Starts an element.
|
void |
startElement(XmlNamespace namespace,
java.lang.String name,
java.util.Collection<XmlWriter.Attribute> attrs,
java.util.Collection<? extends XmlNamespace> namespaceDecls)
Starts an element.
|
void |
startRepeatingElement()
Indicates that a series of repeating elements are about to
be written.
|
void |
writeUnescaped(java.lang.String s)
Writes a string without XML entity escaping.
|
public XmlWriter(java.io.Writer w, java.util.Set<XmlWriter.WriterFlags> f, java.lang.String encoding, boolean standalone) throws java.io.IOException
w
- output writer object.f
- writer configuration flags or null for no flags
encoding
- charset encoding.standalone
- boolean where true=yes and false=no.java.io.IOException
- thrown by the underlying writerXmlWriter.WriterFlags
public XmlWriter(java.io.Writer w, java.util.Set<XmlWriter.WriterFlags> f, java.lang.String encoding) throws java.io.IOException
w
- output writer object.f
- writer configuration flags or null for no flags
encoding
- charset encoding. When non-null, implicitly causes
the WRITE_HEADER flag to be set.java.io.IOException
- thrown by the underlying writer.XmlWriter.WriterFlags
public XmlWriter(java.io.Writer w) throws java.io.IOException
Writer
.w
- output writer object.java.io.IOException
- thrown by the underlying writer.public XmlWriter(java.io.Writer w, java.lang.String encoding) throws java.io.IOException
w
- Output writer object.encoding
- output encoding to use in declaration.java.io.IOException
- thrown by the underlying writer.@Deprecated public XmlWriter(java.io.Writer w, boolean includeHeader) throws java.io.IOException
XmlWriter(Writer, Set, String)
java.io.IOException
public void close() throws java.io.IOException
java.io.IOException
- thrown by the underlying writer.public void flush() throws java.io.IOException
java.io.IOException
- thrown by the underlying writer.public void setDefaultNamespace(XmlNamespace namespace)
namespace
- the new namespace to set as the default at the start
of the next element.public void startElement(java.lang.String name) throws java.io.IOException
name
- element name.java.io.IOException
public void startElement(XmlNamespace namespace, java.lang.String name, java.util.Collection<XmlWriter.Attribute> attrs, java.util.Collection<? extends XmlNamespace> namespaceDecls) throws java.io.IOException
namespace
- element namespace.name
- element name.attrs
- attributes. Can be null
.namespaceDecls
- extra namespace declarations. Can be null
.java.io.IOException
- thrown by the underlying writer.public void endElement(XmlNamespace namespace, java.lang.String name) throws java.io.IOException
namespace
- element namespace.name
- element name.java.io.IOException
public void endElement() throws java.io.IOException
java.io.IOException
public void simpleElement(java.lang.String name, java.lang.String value) throws java.io.IOException
name
- element name.value
- element value. Can be null
.java.io.IOException
- thrown by the underlying writer.public void startRepeatingElement() throws java.io.IOException
java.io.IOException
public void endRepeatingElement() throws java.io.IOException
java.io.IOException
public void simpleElement(XmlNamespace namespace, java.lang.String name, java.util.List<XmlWriter.Attribute> attrs, java.lang.String value) throws java.io.IOException
namespace
- element namespace.name
- element name.attrs
- attributes. Can be null
.value
- element value. Can be null
.java.io.IOException
- thrown by the underlying writer.public void characters(java.lang.String s) throws java.io.IOException
s
- string to emit. Can be null
.java.io.IOException
- thrown by the underlying writer.public void characters(java.lang.String s, boolean useCData) throws java.io.IOException
s
- string to emit. Can be null
.useCData
- CDATA used if true, XML escaping if falsejava.io.IOException
- thrown by the underlying writer.public void innerXml(java.lang.String xml) throws java.io.IOException
xml
- XML blob string.java.io.IOException
- thrown by the underlying writer.public void writeUnescaped(java.lang.String s) throws java.io.IOException
s
- the raw content to write without escaping.java.io.IOException
- thrown by the underlying writer.