public class XmlWriter extends Object implements AutoCloseable
This class wraps the XMLStreamWriter
class so that the methods don’t send checked exceptions, in order to
simplify its usage together with streams and lambdas.
Constructor and Description |
---|
XmlWriter(File file,
boolean indent)
Creates a writer that will write to the given file, using UTF-8 as the encoding.
|
XmlWriter(OutputStream out,
boolean indent)
Creates an XML writer that will write to the given stream, using UTF-8 as the encoding.
|
XmlWriter(Result result,
boolean indent)
Creates an XML writer that will write to the given result, using UTF-8 as the encoding.
|
XmlWriter(Writer out,
boolean indent)
Creates an XML writer that will write to the given writer.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the XML document and the underlying result.
|
void |
flush()
Flushes the output.
|
static String |
renderBoolean(boolean value)
Render a string representation of boolean value
|
static String |
renderDate(Date value)
Render a string representation of Date value
|
static String |
renderDecimal(BigDecimal value)
Render a string representation of BigDecimal value
|
static String |
renderInteger(BigInteger value)
Render a string representation of BigInteger value
|
static String |
renderString(String value)
Render a string representation of string value
|
void |
setPrefix(String prefix,
String uri)
Associates a namespace prefix with a namespace URI.
|
void |
writeAttribute(String name,
String value)
Writes an XML attribute with the given name and value.
|
void |
writeBoolean(String name,
boolean value)
Writes a boolean value.
|
void |
writeBooleans(String name,
List<Boolean> values)
Writes a list of boolean values.
|
void |
writeCharacters(String text)
Writes the given characters as text content.
|
void |
writeDate(String name,
Date value)
Writes a date.
|
void |
writeDates(String name,
List<Date> values)
Writes a list of date values.
|
void |
writeDecimal(String name,
BigDecimal value)
Writes a decimal value.
|
void |
writeDecimals(String name,
List<BigDecimal> values)
Writes a list of decimal values.
|
void |
writeElement(String name,
String value)
Writes an XML element with the given name and value.
|
void |
writeElement(String uri,
String name,
String value)
Writes an XML element with the given name and value.
|
void |
writeElements(String name,
List<String> values)
Writes a list of string values.
|
void |
writeEndDocument()
Writes the end of the document.
|
void |
writeEndElement()
Closes the latest XML element started with the
writeStartElement(String) . |
void |
writeInteger(String name,
BigInteger value)
Writes an integer value.
|
void |
writeIntegers(String name,
List<BigInteger> values)
Writes a list of integer values.
|
void |
writeLine()
Writes a blank line.
|
void |
writeStartDocument(String encoding,
String version)
Writes the start of the document.
|
void |
writeStartElement(String name)
Writes the start of an XML element with the given name.
|
void |
writeStartElement(String uri,
String name)
Writes the start of an XML element with the given name.
|
void |
writeString(String name,
String value)
Writes a string.
|
public XmlWriter(Result result, boolean indent)
Creates an XML writer that will write to the given result, using UTF-8 as the encoding.
result
- the result where the document will be writtenindent
- indicates if the output should be indentedpublic XmlWriter(OutputStream out, boolean indent)
Creates an XML writer that will write to the given stream, using UTF-8 as the encoding.
out
- the stream where the document will be writtenindent
- indicates if the output should be indentedpublic XmlWriter(Writer out, boolean indent)
Creates an XML writer that will write to the given writer.
out
- the writer where the document will be writtenindent
- indicates if the output should be indentedpublic XmlWriter(File file, boolean indent)
Creates a writer that will write to the given file, using UTF-8 as the encoding.
file
- the file where the document will be writtenindent
- indicates if the output should be indentedpublic void writeStartDocument(String encoding, String version)
Writes the start of the document.
encoding
- the character encoding used in the documentversion
- the XML version used in the documentpublic void writeEndDocument()
Writes the end of the document.
public void setPrefix(String prefix, String uri)
Associates a namespace prefix with a namespace URI.
prefix
- the namespace prefixuri
- the namespace URIpublic void writeElement(String uri, String name, String value)
Writes an XML element with the given name and value. For example, if the name is size
and the value is
100
it will write <size>100</size>
.
uri
- the namespace URIname
- the name of the XML elementvalue
- the text content of the XML elementpublic void writeElement(String name, String value)
Writes an XML element with the given name and value. For example, if the name is size
and the value is
100
it will write <size>100</size>
.
name
- the name of the XML elementvalue
- the text content of the XML elementpublic void writeStartElement(String uri, String name)
Writes the start of an XML element with the given name. For example, if the name is size
it will
write <size>
.
uri
- the namespace URIname
- the name of the XML elementpublic void writeStartElement(String name)
Writes the start of an XML element with the given name. For example, if the name is size
it will
write <size>
.
name
- the name of the XML elementpublic void writeEndElement()
Closes the latest XML element started with the writeStartElement(String)
.
public void writeAttribute(String name, String value)
Writes an XML attribute with the given name and value. For example, if the name is size
and the value
is 100
it will write size="100"
.
name
- the name of the XML attributevalue
- the text content of the XML attributepublic void writeCharacters(String text)
Writes the given characters as text content.
text
- the characters to writepublic void writeLine()
Writes a blank line.
public void writeBoolean(String name, boolean value)
Writes a boolean value.
public void writeInteger(String name, BigInteger value)
Writes an integer value.
public void writeDecimal(String name, BigDecimal value)
Writes a decimal value.
public void writeBooleans(String name, List<Boolean> values)
Writes a list of boolean values.
public void writeIntegers(String name, List<BigInteger> values)
Writes a list of integer values.
public void writeDecimals(String name, List<BigDecimal> values)
Writes a list of decimal values.
public void writeElements(String name, List<String> values)
Writes a list of string values.
public static String renderString(String value)
Render a string representation of string value
public static String renderBoolean(boolean value)
Render a string representation of boolean value
public static String renderInteger(BigInteger value)
Render a string representation of BigInteger value
public static String renderDecimal(BigDecimal value)
Render a string representation of BigDecimal value
public void flush()
Flushes the output.
public void close()
Closes the XML document and the underlying result.
close
in interface AutoCloseable
Copyright © 2016. All rights reserved.