|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.gdata.util.common.xml.XmlWriter
public class XmlWriter
Implements a simple XML writer on top of java.io.PrintWriter. This implementation can be conveniently used to generate XML responses in servlets.
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>
Nested Class Summary | |
---|---|
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. |
Field Summary | |
---|---|
protected java.lang.String |
encoding
Encoding of output |
protected java.util.Set<XmlWriter.WriterFlags> |
flags
|
protected java.io.Writer |
writer
The underlying output Writer associated with this XmlWriter. |
Constructor Summary | |
---|---|
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. see XmlWriter(Writer, Set, String) |
|
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. |
Method Summary | |
---|---|
void |
characters(java.lang.String s)
Emits character data subject to XML escaping. |
void |
close()
Closes the XmlWriter and the underlying output writer. |
protected XmlWriter.Element |
createElement(java.lang.String nsAlias,
java.lang.String nsUri,
java.lang.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,
java.lang.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 java.lang.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 java.lang.String |
getNamespaceUri(java.lang.String nsAlias)
Returns the namespace URI associated with a given namespace alias. |
void |
innerXml(java.lang.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(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. |
protected void |
writeAttribute(java.lang.String name,
java.lang.String value)
Writes an unqualfied XML attribute. |
protected void |
writeAttribute(java.lang.String nsAlias,
java.lang.String name,
java.lang.String value)
Writes a namespace-qualified XML attribute. |
protected void |
writeBeginOutput()
writes beginning of output if any. |
protected void |
writeCloseTag(java.lang.String nsAlias,
java.lang.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(java.lang.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(java.lang.String nsAlias,
java.lang.String name)
Writes the start of the opening tag of an element. |
protected void |
writeQualifiedName(java.lang.String nsAlias,
java.lang.String name)
Writes a namespace qualified element or attribute name. |
void |
writeUnescaped(java.lang.String s)
Writes a string without XML entity escaping. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final java.util.Set<XmlWriter.WriterFlags> flags
protected final java.io.Writer writer
protected java.lang.String encoding
Constructor Detail |
---|
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
Method Detail |
---|
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.protected XmlWriter.Element createElement(java.lang.String nsAlias, java.lang.String nsUri, java.lang.String name)
protected XmlWriter.Element currentElement()
null
if no element is being
written.
protected XmlWriter.Element parentElement()
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.protected boolean shouldWriteHeaderAndFooter()
protected void endOpenTag() throws java.io.IOException
java.io.IOException
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.protected java.lang.String ensureNamespace(XmlNamespace namespace)
null
for the default namespace.protected java.lang.String getNamespaceUri(java.lang.String nsAlias)
nsAlias
- namespace alias, or null
for default namespace.
null
if not found.protected void writeBeginOutput() throws java.io.IOException
java.io.IOException
protected void writeEndOutput() throws java.io.IOException
close()
should not be used because it closes the underlying
stream, which we don't always want to do.
java.io.IOException
protected void writeHeader(java.lang.String enc) throws java.io.IOException
enc
- the XML encoding for the output. Can be null
.
java.io.IOException
- thrown by the underlying writer.protected void writeFooter() throws java.io.IOException
java.io.IOException
protected void writeQualifiedName(java.lang.String nsAlias, java.lang.String name) throws java.io.IOException
nsAlias
- namespace alias prefix.name
- namespace-relative local name.
java.io.IOException
- thrown by the underlying writer.protected void writeOpenTagStart(java.lang.String nsAlias, java.lang.String name) throws java.io.IOException
nsAlias
- namespace alias prefix for the element.name
- tag name for the element.
java.io.IOException
- thrown by the underlying writer.protected void writeOpenTagEnd() throws java.io.IOException
java.io.IOException
- thrown by the underlying writer.protected void writeCloseTag(java.lang.String nsAlias, java.lang.String name) throws java.io.IOException
nsAlias
- namespace alias prefix for the element.name
- tag name for the element.
java.io.IOException
- thrown by the underlying writer.protected void writeAttribute(java.lang.String name, java.lang.String value) throws java.io.IOException
name
- the name of the attribute.value
- the value of the attribute.
java.io.IOException
- thrown by the underlying writer.protected void writeAttribute(java.lang.String nsAlias, java.lang.String name, java.lang.String value) throws java.io.IOException
nsAlias
- namespace alias prefix for the attribute.name
- the name of the attribute.value
- the value of the attribute.
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 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.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |