| java.lang.Object | |
| ↳ | com.google.gdata.util.common.xml.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 Classes | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| XmlWriter.Attribute | The Attribute class represents an XML attribute. | ||||||||||
| XmlWriter.Element | The Element class contains information about an XML element. | ||||||||||
| XmlWriter.Namespace |
This class is deprecated.
Use the XmlNamespace class instead.
|
||||||||||
| XmlWriter.WriterFlags | Enumeration type that can be used to configure the XmlWriter behavior. | ||||||||||
| Constants | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| String | INDENTATION_UNIT | ||||||||||
| Fields | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| defaultNamespace | Current default namespace. | ||||||||||
| elementStack | Stack of currently opened elements. | ||||||||||
| encoding | Encoding of output | ||||||||||
| flags | |||||||||||
| nextDefaultNamespace | |||||||||||
| standalone | |||||||||||
| writer | The underlying output Writer associated with this XmlWriter. | ||||||||||
| Public Constructors | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Constructor that allows standalone directive to be provided.
| |||||||||||
The default namespace that will take effect on the next
element transition.
| |||||||||||
Constructs an XmlWriter instance associated that will generate
XML content to an underlying Writer.
| |||||||||||
Constructor that writers header including encoding information.
| |||||||||||
This constructor is deprecated.
see
XmlWriter(Writer, Set, String)
| |||||||||||
| Public Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Emits character data subject to XML escaping.
| |||||||||||
Emits character data subject to either XML escaping or CDATA escaping.
| |||||||||||
Closes the XmlWriter and the underlying output writer.
| |||||||||||
Ends the current element.
| |||||||||||
Ends the current element.
| |||||||||||
Indicates that the series of repeating elements have been completely
written.
| |||||||||||
Flushes the XmlWriter and the underlying output writer.
| |||||||||||
Writes inner XML provided as a string.
| |||||||||||
Sets the default namespace.
| |||||||||||
Emits a simple element (without child elements).
| |||||||||||
Emits a simple element (without child elements).
| |||||||||||
Starts an element.
| |||||||||||
Starts an element.
| |||||||||||
Indicates that a series of repeating elements are about to
be written.
| |||||||||||
Writes a string without XML entity escaping.
| |||||||||||
| Protected Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
Constructs an Element instance that describes an XML element that
is about to be written.
| |||||||||||
Returns the current element, or
null if no element is being
written. | |||||||||||
Ends the start tag for an element.
| |||||||||||
Ensures the namespace is in scope and returns its alias.
| |||||||||||
Returns the namespace URI associated with a given namespace alias.
| |||||||||||
Return parent of current element.
| |||||||||||
Tests whether header and footer should be included in output
| |||||||||||
Writes an unqualfied XML attribute.
| |||||||||||
Writes a namespace-qualified XML attribute.
| |||||||||||
writes beginning of output if any.
| |||||||||||
Writes the closing tag of an element, after all nested elements and
value text have been written.
| |||||||||||
Writes any closing information to the output writer.
| |||||||||||
Writes footer, if any, that corresponds to the header.
| |||||||||||
Writes basic XML headers including XML version, standalone, and encoding.
| |||||||||||
Writes the end of the opening tag of an element after all attributes
have been written.
| |||||||||||
Writes the start of the opening tag of an element.
| |||||||||||
Writes a namespace qualified element or attribute name.
| |||||||||||
|
[Expand]
Inherited Methods | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
Current default namespace.
Encoding of output
The underlying output Writer associated with this XmlWriter.
Constructor that allows standalone directive to be provided. Please note that using this constructor explicity causes the standalone header value to be written and WRITE_HEADER flag to be set.
| w | Output writer object. |
|---|---|
| f | Writer configuration flags or null for no flags |
| encoding | Charset encoding. |
| standalone | Boolean where true=yes and false=no. |
| IOException | thrown by the underlying writer |
|---|
The default namespace that will take effect on the next element transition.
| 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. |
| IOException | thrown by the underlying writer. |
|---|
Constructs an XmlWriter instance associated that will generate XML content to an underlying Writer.
| w | Output writer object. |
|---|
| IOException | thrown by the underlying writer. |
|---|
Constructor that writers header including encoding information.
| w | Output writer object. |
|---|---|
| encoding | Output encoding to use in declaration. |
| IOException | thrown by the underlying writer. |
|---|
This constructor is deprecated.
see XmlWriter(Writer, Set, String)
| w | |
|---|---|
| includeHeader |
| IOException |
|---|
Emits character data subject to XML escaping.
| s | String to emit. Can be null. |
|---|
| IOException | thrown by the underlying writer. |
|---|
Emits character data subject to either XML escaping or CDATA escaping.
| s | String to emit. Can be null. |
|---|---|
| useCData | CDATA used if true, XML escaping if false |
| IOException | thrown by the underlying writer. |
|---|
Closes the XmlWriter and the underlying output writer.
| IOException | thrown by the underlying writer. |
|---|
Ends the current element. It is expected to be on top of the stack.
| namespace | Element namespace. |
|---|---|
| name | Element name. |
| IOException |
|---|
Ends the current element. No integrity checking is performed.
| IOException |
|---|
Indicates that the series of repeating elements have been completely written.
| IOException |
|---|
Flushes the XmlWriter and the underlying output writer.
| IOException | thrown by the underlying writer. |
|---|
Writes inner XML provided as a string. Used to write out XML blobs.
| xml | XML blob string. |
|---|
| IOException | thrown by the underlying writer. |
|---|
Sets the default namespace. It takes effect on the next element.
| namespace | The new namespace to set as the default at the start of the next element. |
|---|
Emits a simple element (without child elements).
| name | Element name. |
|---|---|
| value | Element value. Can be null. |
| IOException | thrown by the underlying writer. |
|---|
Emits a simple element (without child elements).
| namespace | Element namespace. |
|---|---|
| name | Element name. |
| attrs | Attributes. Can be null. |
| value | Element value. Can be null. |
| IOException | thrown by the underlying writer. |
|---|
Starts an element. This element can be a parent to other elements.
| namespace | Element namespace. |
|---|---|
| name | Element name. |
| attrs | Attributes. Can be null. |
| namespaceDecls | Extra namespace declarations. Can be null. |
| IOException | thrown by the underlying writer. |
|---|
Starts an element. This element can be a parent to other elements.
| name | Element name. |
|---|
| IOException |
|---|
Indicates that a series of repeating elements are about to be written.
| IOException |
|---|
Writes a string without XML entity escaping.
| s | The raw content to write without escaping. |
|---|
| IOException | thrown by the underlying writer. |
|---|
Constructs an Element instance that describes an XML element that is about to be written.
| nsAlias | |
|---|---|
| nsUri | |
| name |
Returns the current element, or null if no element is being
written.
Ends the start tag for an element.
| IOException |
|---|
Ensures the namespace is in scope and returns its alias. The top of the stack is assumed to be the current element. If the namespace is not found, it is appended to the current element.
| namespace |
|---|
null for the default namespace.
Returns the namespace URI associated with a given namespace alias.
| nsAlias | Namespace alias, or null for default namespace. |
|---|
null if not found.
Return parent of current element. Stack has a dummy root element so every real element has non-null a parent.
Tests whether header and footer should be included in output
Writes an unqualfied XML attribute.
| name | The name of the attribute. |
|---|---|
| value | The value of the attribute. |
| IOException | thrown by the underlying writer. |
|---|
Writes a namespace-qualified XML attribute.
| 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. |
|---|
writes beginning of output if any.
| IOException |
|---|
Writes the closing tag of an element, after all nested elements and value text have been written.
| nsAlias | Namespace alias prefix for the element. |
|---|---|
| name | Tag name for the element. |
| IOException | thrown by the underlying writer. |
|---|
Writes any closing information to the output writer. This is used by
subclasses to write anything they need to close out the object.
close() should not be used because it closes the underlying
stream, which we don't always want to do.
| IOException |
|---|
Writes footer, if any, that corresponds to the header.
| IOException |
|---|
Writes basic XML headers including XML version, standalone, and encoding.
| enc | The XML encoding for the output. Can be null. |
|---|
| IOException | thrown by the underlying writer. |
|---|
Writes the end of the opening tag of an element after all attributes have been written.
| IOException | thrown by the underlying writer. |
|---|
Writes the start of the opening tag of an element.
| nsAlias | Namespace alias prefix for the element. |
|---|---|
| name | Tag name for the element. |
| IOException | thrown by the underlying writer. |
|---|
Writes a namespace qualified element or attribute name.
| nsAlias | Namespace alias prefix. |
|---|---|
| name | Namespace-relative local name. |
| IOException | thrown by the underlying writer. |
|---|