public class XmlWriter extends Object
Modifier and Type | Field and Description |
---|---|
static String |
CDATA_END
CDATA end tag: "]]>"
|
static String |
CDATA_START
CDATA start tag: "
|
static String |
DEFAULT_ENCODING
Default encoding value which is "UTF-8"
|
Constructor and Description |
---|
XmlWriter(OutputStream outputStream,
String encoding)
Create an XmlWriter on top of an existing
OutputStream . |
XmlWriter(Writer writer)
Create an XmlWriter on top of an existing java.io.Writer.
|
XmlWriter(Writer writer,
String encoding)
Create an XmlWriter on top of an existing java.io.Writer.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close this writer.
|
protected String |
convertCharacterToEntity(char currentChar,
boolean literally) |
void |
enablePrettyPrint(boolean enable)
Turn pretty printing on or off.
|
XmlWriter |
endElement()
End the current element.
|
static void |
main(String[] args) |
void |
setIndent(String indent)
Specify the string to prepend to a line for each level of indent.
|
void |
setNewline(String newline)
Specify the string used to terminate each line when pretty printing.
|
void |
setWriter(Writer writer,
String encoding)
Resets the handler to write a new text document.
|
static void |
test1() |
static void |
test2() |
XmlWriter |
writeAttribute(String attr,
String value)
Write an attribute out for the current element.
|
XmlWriter |
writeAttribute(String attr,
String value,
boolean literally)
Write an attribute out for the current element.
|
XmlWriter |
writeCData(String cdata)
Write out a chunk of CDATA.
|
XmlWriter |
writeComment(String comment)
Write out a chunk of comment.
|
XmlWriter |
writeDeclaration() |
XmlWriter |
writeDoctype(String systemId,
String publicId) |
XmlWriter |
writeElement(String name)
Begin to write out an element.
|
XmlWriter |
writeElementWithText(String name,
String text)
A helper method.
|
XmlWriter |
writeEmptyElement(String name)
A helper method.
|
XmlWriter |
writeText(String text)
Output body text.
|
XmlWriter |
writeText(String text,
boolean literally)
Output body text.
|
public static final String CDATA_START
public static final String CDATA_END
public static final String DEFAULT_ENCODING
public XmlWriter(Writer writer)
public XmlWriter(Writer writer, String encoding)
public XmlWriter(OutputStream outputStream, String encoding) throws UnsupportedEncodingException
OutputStream
.outputStream
- encoding
- The encoding to be used for writing to the given output
stream. Can be null
. If it is null
the DEFAULT_ENCODING
is used.UnsupportedEncodingException
public void enablePrettyPrint(boolean enable)
enable
- true to enable, false to disable pretty printing.public void setIndent(String indent)
indent
- representing one level of indentation while pretty printing.public void setNewline(String newline)
newline
- representing the newline sequence when pretty printing.public XmlWriter writeElementWithText(String name, String text) throws IOException
name
- String name of tagtext
- String of text to go inside the tagIOException
public XmlWriter writeEmptyElement(String name) throws IOException
name
- String name of tagIOException
public XmlWriter writeElement(String name) throws IOException
name
- String name of tagIOException
public XmlWriter writeAttribute(String attr, String value) throws IOException
attr
- name of attribute.value
- value of attribute.IOException
writeAttribute(String, String, boolean)
public XmlWriter writeAttribute(String attr, String value, boolean literally) throws IOException
attr
- name of attribute.value
- value of attribute.literally
- If the writer should be literally on the given value which
means that meta characters will also be preserved by escaping
them. Mainly preserves newlines and tabs.IOException
public XmlWriter endElement() throws IOException
IOException
public void close() throws IOException
IOException
public XmlWriter writeText(String text) throws IOException
text
- The text to be writtenIOException
writeText(String, boolean)
public XmlWriter writeText(String text, boolean literally) throws IOException
text
- The text to be writtenliterally
- If the writer should be literally on the given value which
means that meta characters will also be preserved by escaping
them. Mainly preserves newlines and tabs.IOException
public XmlWriter writeCData(String cdata) throws IOException
cdata
- of CDATA text.IOException
public XmlWriter writeComment(String comment) throws IOException
comment
- of text to comment.IOException
public static void main(String[] args) throws IOException
IOException
public static void test1() throws IOException
IOException
public static void test2() throws IOException
IOException
protected String convertCharacterToEntity(char currentChar, boolean literally)
public final void setWriter(Writer writer, String encoding)
writer
- XML text is written to this writer.encoding
- if non-null, and an XML declaration is written, this is the
name that will be used for the character encoding.IllegalStateException
- if the current document hasn't yet ended (i.e. the output
stream out
is not null)public XmlWriter writeDeclaration() throws IOException
IOException
public XmlWriter writeDoctype(String systemId, String publicId) throws IOException
IOException
Copyright © 2002–2024. All rights reserved.