public class XmlWriter extends 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.
|
protected static class |
XmlWriter.Element
The Element class contains information about an XML element.
|
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.
|
| Modifier and Type | Field and Description |
|---|---|
protected String |
encoding
Encoding of output
|
protected Set<XmlWriter.WriterFlags> |
flags |
protected Writer |
writer
The underlying output Writer associated with this XmlWriter.
|
| Constructor and Description |
|---|
XmlWriter(Writer w)
Constructs an XmlWriter instance associated that will generate
XML content to an underlying
Writer. |
XmlWriter(Writer w,
boolean includeHeader)
Deprecated.
|
XmlWriter(Writer w,
Set<XmlWriter.WriterFlags> f,
String encoding)
The default namespace that will take effect on the next
element transition.
|
XmlWriter(Writer w,
Set<XmlWriter.WriterFlags> f,
String encoding,
boolean standalone)
Constructor that allows standalone directive to be provided.
|
XmlWriter(Writer w,
String encoding)
Constructor that writers header including encoding information.
|
| Modifier and Type | Method and Description |
|---|---|
void |
characters(String s)
Emits character data subject to XML escaping.
|
void |
characters(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.
|
protected XmlWriter.Element |
createElement(String nsAlias,
String nsUri,
String name)
Constructs an Element instance that describes an XML element that
is about to be written.
|
protected XmlWriter.Element |
currentElement()
Returns the current element, or
null if no element is being
written. |
void |
endElement()
Ends the current element.
|
void |
endElement(XmlNamespace namespace,
String name)
Ends the current element.
|
protected void |
endOpenTag()
Ends the start tag for an element.
|
void |
endRepeatingElement()
Indicates that the series of repeating elements have been completely
written.
|
protected String |
ensureNamespace(XmlNamespace namespace)
Ensures the namespace is in scope and returns its alias.
|
void |
flush()
Flushes the XmlWriter and the underlying output writer.
|
protected String |
getNamespaceUri(String nsAlias)
Returns the namespace URI associated with a given namespace alias.
|
void |
innerXml(String xml)
Writes inner XML provided as a string.
|
protected XmlWriter.Element |
parentElement()
Return parent of current element.
|
void |
setDefaultNamespace(XmlNamespace namespace)
Sets the default namespace.
|
protected boolean |
shouldWriteHeaderAndFooter()
Tests whether header and footer should be included in output
|
void |
simpleElement(String name,
String value)
Emits a simple element (without child elements).
|
void |
simpleElement(XmlNamespace namespace,
String name,
List<XmlWriter.Attribute> attrs,
String value)
Emits a simple element (without child elements).
|
void |
startElement(String name)
Starts an element.
|
void |
startElement(XmlNamespace namespace,
String name,
Collection<XmlWriter.Attribute> attrs,
Collection<? extends XmlNamespace> namespaceDecls)
Starts an element.
|
void |
startRepeatingElement()
Indicates that a series of repeating elements are about to
be written.
|
protected void |
writeAttribute(String name,
String value)
Writes an unqualfied XML attribute.
|
protected void |
writeAttribute(String nsAlias,
String name,
String value)
Writes a namespace-qualified XML attribute.
|
protected void |
writeBeginOutput()
writes beginning of output if any.
|
protected void |
writeCloseTag(String nsAlias,
String name)
Writes the closing tag of an element, after all nested elements and
value text have been written.
|
protected void |
writeEndOutput()
Writes any closing information to the output writer.
|
protected void |
writeFooter()
Writes footer, if any, that corresponds to the header.
|
protected void |
writeHeader(String enc)
Writes basic XML headers including XML version, standalone, and encoding.
|
protected void |
writeOpenTagEnd()
Writes the end of the opening tag of an element after all attributes
have been written.
|
protected void |
writeOpenTagStart(String nsAlias,
String name)
Writes the start of the opening tag of an element.
|
protected void |
writeQualifiedName(String nsAlias,
String name)
Writes a namespace qualified element or attribute name.
|
void |
writeUnescaped(String s)
Writes a string without XML entity escaping.
|
protected final Set<XmlWriter.WriterFlags> flags
protected final Writer writer
protected String encoding
public XmlWriter(Writer w, Set<XmlWriter.WriterFlags> f, String encoding, boolean standalone) throws IOException
w - output writer object.f - writer configuration flags or null for no flagsencoding - charset encoding.standalone - boolean where true=yes and false=no.IOException - thrown by the underlying writerXmlWriter.WriterFlagspublic XmlWriter(Writer w, Set<XmlWriter.WriterFlags> f, String encoding) throws IOException
w - output writer object.f - writer configuration flags or null for no flagsencoding - charset encoding. When non-null, implicitly causes
the WRITE_HEADER flag to be set.IOException - thrown by the underlying writer.XmlWriter.WriterFlagspublic XmlWriter(Writer w) throws IOException
Writer.w - output writer object.IOException - thrown by the underlying writer.public XmlWriter(Writer w, String encoding) throws IOException
w - Output writer object.encoding - output encoding to use in declaration.IOException - thrown by the underlying writer.@Deprecated public XmlWriter(Writer w, boolean includeHeader) throws IOException
XmlWriter(Writer, Set, String)IOExceptionpublic void close()
throws IOException
IOException - thrown by the underlying writer.public void flush()
throws IOException
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.protected XmlWriter.Element createElement(String nsAlias, String nsUri, String name)
protected XmlWriter.Element currentElement()
null if no element is being
written.protected XmlWriter.Element parentElement()
public void startElement(String name) throws IOException
name - element name.IOExceptionpublic void startElement(XmlNamespace namespace, String name, Collection<XmlWriter.Attribute> attrs, Collection<? extends XmlNamespace> namespaceDecls) throws IOException
namespace - element namespace.name - element name.attrs - attributes. Can be null.namespaceDecls - extra namespace declarations. Can be null.IOException - thrown by the underlying writer.protected boolean shouldWriteHeaderAndFooter()
protected void endOpenTag()
throws IOException
IOExceptionpublic void endElement(XmlNamespace namespace, String name) throws IOException
namespace - element namespace.name - element name.IOExceptionpublic void endElement()
throws IOException
IOExceptionpublic void simpleElement(String name, String value) throws IOException
name - element name.value - element value. Can be null.IOException - thrown by the underlying writer.public void startRepeatingElement()
throws IOException
IOExceptionpublic void endRepeatingElement()
throws IOException
IOExceptionpublic void simpleElement(XmlNamespace namespace, String name, List<XmlWriter.Attribute> attrs, String value) throws IOException
namespace - element namespace.name - element name.attrs - attributes. Can be null.value - element value. Can be null.IOException - thrown by the underlying writer.protected String ensureNamespace(XmlNamespace namespace)
null for the default namespace.protected String getNamespaceUri(String nsAlias)
nsAlias - namespace alias, or null for default namespace.null if not found.protected void writeBeginOutput()
throws IOException
IOExceptionprotected void writeEndOutput()
throws IOException
close() should not be used because it closes the underlying
stream, which we don't always want to do.IOExceptionprotected void writeHeader(String enc) throws IOException
enc - the XML encoding for the output. Can be null.IOException - thrown by the underlying writer.protected void writeFooter()
throws IOException
IOExceptionprotected void writeQualifiedName(String nsAlias, String name) throws IOException
nsAlias - namespace alias prefix.name - namespace-relative local name.IOException - thrown by the underlying writer.protected void writeOpenTagStart(String nsAlias, String name) throws IOException
nsAlias - namespace alias prefix for the element.name - tag name for the element.IOException - thrown by the underlying writer.protected void writeOpenTagEnd()
throws IOException
IOException - thrown by the underlying writer.protected void writeCloseTag(String nsAlias, String name) throws IOException
nsAlias - namespace alias prefix for the element.name - tag name for the element.IOException - thrown by the underlying writer.protected void writeAttribute(String name, String value) throws IOException
name - the name of the attribute.value - the value of the attribute.IOException - thrown by the underlying writer.protected void writeAttribute(String nsAlias, String name, String value) throws IOException
nsAlias - namespace alias prefix for the attribute.name - the name of the attribute.value - the value of the attribute.IOException - thrown by the underlying writer.public void characters(String s) throws IOException
s - string to emit. Can be null.IOException - thrown by the underlying writer.public void characters(String s, boolean useCData) throws IOException
s - string to emit. Can be null.useCData - CDATA used if true, XML escaping if falseIOException - thrown by the underlying writer.public void innerXml(String xml) throws IOException
xml - XML blob string.IOException - thrown by the underlying writer.public void writeUnescaped(String s) throws IOException
s - the raw content to write without escaping.IOException - thrown by the underlying writer.Copyright © 2012. All Rights Reserved.