public class XMLWriter extends BufferedWriter
This writer will do for creating simple XML files, but please note that it is not
perfect at all. It does not support namespace, DTDs and it is unable to read DOM
structures as in the org.w3c.dom package. And last but not least it does
not validate your XML against a DTD.
What it does is indention of the text, proper escaping of XML special chars and correct charset encoding.
| Constructor and Description |
|---|
XMLWriter(OutputStream out)
Creates a new XMLWriter basing on an
OutputStream. |
XMLWriter(Writer out)
Creates a new XMLWriter basing on a
Writer. |
| Modifier and Type | Method and Description |
|---|---|
void |
endDocument()
Finishes an XML document.
|
void |
endElement()
Closes the most recent XML element.
|
static String |
escape(String text)
Escapes a String so it can be used in XML context.
|
protected void |
flushTag()
Flushes the tag buffer, which is used to write empty containers in their short
form.
|
void |
setEncoding(String encoding)
Sets the encoding used by this writer.
|
void |
setIndent(String indent)
Sets the indention string.
|
void |
startDocument()
Starts an XML document.
|
void |
startElement(String element)
Starts a new XML element.
|
void |
startElement(String element,
Attributes attr)
Starts a new XML element with attributes.
|
void |
startElement(String element,
Map<String,String> attrs)
Starts a new XML element with attributes.
|
void |
startElement(String element,
String... attr)
Starts a new XML element with attributes.
|
void |
writeComment(String comment)
Writes a comment.
|
void |
writeContent(String content)
Writes the content of an XML container.
|
protected void |
writeIndent()
Indents by one level
|
public XMLWriter(Writer out)
Writer.out - Writer to send the XML data topublic XMLWriter(OutputStream out) throws UnsupportedEncodingException
OutputStream. The OutputStream
will receive UTF-8 encoded data.out - OutputStream to send the XML data toUnsupportedEncodingException - if this VM does not support UTF-8, which should never happen...public void setIndent(String indent)
If you want to change the indention string, you must do so before invoking
startDocument().
indent - New Indention Stringpublic void setEncoding(String encoding)
If you want to change the encoding string, you must do so before invoking
startDocument().
encoding - New encodingprotected void writeIndent() throws IOException
IOExceptionpublic void startDocument() throws IOException
IOExceptionpublic void endDocument() throws IOException
IOExceptionpublic void startElement(String element) throws IOException
element - Element nameIOExceptionpublic void startElement(String element, String... attr) throws IOException
element - Element nameattr - Attribute name/value pairsIOExceptionpublic void startElement(String element, Map<String,String> attrs) throws IOException
Note that since R15, the map is required to have String keys and values.
element - Element nameattrs - Map with attributes and corresponding valuesIOExceptionpublic void startElement(String element, Attributes attr) throws IOException
element - Element nameattr - AttributesIOExceptionpublic void endElement() throws IOException
IOExceptionpublic void writeContent(String content) throws IOException
content - Content of the current XML containerIOExceptionpublic void writeComment(String comment) throws IOException
comment - The comment's content.IOExceptionpublic static String escape(String text)
text - Text to be escapedprotected void flushTag() throws IOException
IOExceptionCopyright © 2004–2016. All rights reserved.