Class XmlWriter

  • All Implemented Interfaces:
    AutoCloseable

    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 Detail

      • XmlWriter

        public XmlWriter​(Result result,
                         boolean indent)
        Creates an XML writer that will write to the given result, using UTF-8 as the encoding.
        Parameters:
        result - the result where the document will be written
        indent - indicates if the output should be indented
      • XmlWriter

        public XmlWriter​(OutputStream out,
                         boolean indent)
        Creates an XML writer that will write to the given stream, using UTF-8 as the encoding.
        Parameters:
        out - the stream where the document will be written
        indent - indicates if the output should be indented
      • XmlWriter

        public XmlWriter​(Writer out,
                         boolean indent)
        Creates an XML writer that will write to the given writer.
        Parameters:
        out - the writer where the document will be written
        indent - indicates if the output should be indented
      • XmlWriter

        public XmlWriter​(File file,
                         boolean indent)
        Creates a writer that will write to the given file, using UTF-8 as the encoding.
        Parameters:
        file - the file where the document will be written
        indent - indicates if the output should be indented
    • Method Detail

      • writeStartDocument

        public void writeStartDocument​(String encoding,
                                       String version)
        Writes the start of the document.
        Parameters:
        encoding - the character encoding used in the document
        version - the XML version used in the document
      • writeEndDocument

        public void writeEndDocument()
        Writes the end of the document.
      • setPrefix

        public void setPrefix​(String prefix,
                              String uri)
        Associates a namespace prefix with a namespace URI.
        Parameters:
        prefix - the namespace prefix
        uri - the namespace URI
      • writeElement

        public 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>.
        Parameters:
        uri - the namespace URI
        name - the name of the XML element
        value - the text content of the XML element
      • writeElement

        public 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>.
        Parameters:
        name - the name of the XML element
        value - the text content of the XML element
      • writeStartElement

        public 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>.
        Parameters:
        uri - the namespace URI
        name - the name of the XML element
      • writeStartElement

        public 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>.
        Parameters:
        name - the name of the XML element
      • writeAttribute

        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".
        Parameters:
        name - the name of the XML attribute
        value - the text content of the XML attribute
      • writeCharacters

        public void writeCharacters​(String text)
        Writes the given characters as text content.
        Parameters:
        text - the characters to write
      • writeLine

        public void writeLine()
        Writes a blank line.
      • writeBoolean

        public void writeBoolean​(String name,
                                 boolean value)
        Writes a boolean value.
      • writeInteger

        public void writeInteger​(String name,
                                 BigInteger value)
        Writes an integer value.
      • writeDecimal

        public void writeDecimal​(String name,
                                 BigDecimal value)
        Writes a decimal value.
      • writeDate

        public void writeDate​(String name,
                              Date value)
        Writes a date.
      • writeString

        public void writeString​(String name,
                                String value)
        Writes a string.
      • writeBooleans

        public void writeBooleans​(String name,
                                  List<Boolean> values)
        Writes a list of boolean values.
      • writeIntegers

        public void writeIntegers​(String name,
                                  List<BigInteger> values)
        Writes a list of integer values.
      • writeDecimals

        public void writeDecimals​(String name,
                                  List<BigDecimal> values)
        Writes a list of decimal values.
      • writeDates

        public void writeDates​(String name,
                               List<Date> values)
        Writes a list of date values.
      • writeElements

        public void writeElements​(String name,
                                  List<String> values)
        Writes a list of string values.
      • renderString

        public static String renderString​(String value)
        Render a string representation of string value
      • renderBoolean

        public static String renderBoolean​(boolean value)
        Render a string representation of boolean value
      • renderInteger

        public static String renderInteger​(BigInteger value)
        Render a string representation of BigInteger value
      • renderDecimal

        public static String renderDecimal​(BigDecimal value)
        Render a string representation of BigDecimal value
      • renderDate

        public static String renderDate​(Date value)
        Render a string representation of Date value
      • renderOther

        public static String renderOther​(Object value)
        Render a string representation of any value other than the standard: String, Boolean, Integer, Decimal, Date by defaulting to .toString()
      • flush

        public void flush()
        Flushes the output.
      • close

        public void close()
        Closes the XML document and the underlying result.
        Specified by:
        close in interface AutoCloseable