Class XmlWriter


  • public class XmlWriter
    extends XmlWriterSupport
    A class for writing XML to a character stream.
    Author:
    Thomas Morgner
    • Constructor Detail

      • XmlWriter

        public XmlWriter​(java.io.Writer writer)
        Creates a new XML writer for the specified character stream. By default, four spaces are used for indentation.
        Parameters:
        writer - the character stream.
      • XmlWriter

        public XmlWriter​(java.io.Writer writer,
                         TagDescription tagDescription)
        Default Constructor. The created XMLWriterSupport will not have no safe tags and starts with an indention level of 0.
        Parameters:
        writer - the character stream.
        tagDescription - the tags that are safe for line breaks.
      • XmlWriter

        public XmlWriter​(java.io.Writer writer,
                         java.lang.String indentString)
        Creates a new XML writer for the specified character stream.
        Parameters:
        writer - the character stream.
        indentString - the string used for indentation (should contain white space, for example four spaces).
      • XmlWriter

        public XmlWriter​(java.io.Writer writer,
                         TagDescription tagDescription,
                         java.lang.String indentString)
        Creates a new XMLWriter instance.
        Parameters:
        writer - the character stream.
        tagDescription - the tags that are safe for line breaks.
        indentString - the indent string.
      • XmlWriter

        public XmlWriter​(java.io.Writer writer,
                         TagDescription tagDescription,
                         java.lang.String indentString,
                         java.lang.String lineSeparator)
        Creates a new XMLWriter instance.
        Parameters:
        writer - the character stream.
        tagDescription - the tags that are safe for line breaks.
        indentString - the indent string.
        lineSeparator - the line separator to be used.
    • Method Detail

      • writeXmlDeclaration

        public void writeXmlDeclaration​(java.lang.String encoding)
                                 throws java.io.IOException
        Writes the XML declaration that usually appears at the top of every XML file.
        Parameters:
        encoding - the encoding that should be declared (this has to match the encoding of the writer, or funny things may happen when parsing the xml file later).
        Throws:
        java.io.IOException - if there is a problem writing to the character stream.
      • writeTag

        public void writeTag​(java.lang.String namespace,
                             java.lang.String name,
                             boolean close)
                      throws java.io.IOException
        Writes an opening XML tag that has no attributes.
        Parameters:
        namespace - the namespace URI for the element
        name - the tag name.
        close - a flag that controls whether or not the tag is closed immediately.
        Throws:
        java.io.IOException - if there is an I/O problem.
      • writeCloseTag

        public void writeCloseTag()
                           throws java.io.IOException
        Writes a closing XML tag.
        Throws:
        java.io.IOException - if there is an I/O problem.
      • writeTag

        public void writeTag​(java.lang.String namespace,
                             java.lang.String name,
                             java.lang.String attributeName,
                             java.lang.String attributeValue,
                             boolean close)
                      throws java.io.IOException
        Writes an opening XML tag with an attribute/value pair.
        Parameters:
        namespace - the namespace URI for the element
        name - the tag name.
        attributeName - the attribute name.
        attributeValue - the attribute value.
        close - controls whether the tag is closed.
        Throws:
        java.io.IOException - if there is an I/O problem.
      • writeTag

        public void writeTag​(java.lang.String namespace,
                             java.lang.String name,
                             AttributeList attributes,
                             boolean close)
                      throws java.io.IOException
        Writes an opening XML tag along with a list of attribute/value pairs.
        Parameters:
        namespace - the namespace URI for the element
        name - the tag name.
        attributes - the attributes.
        close - controls whether the tag is closed.
        Throws:
        java.io.IOException - if there is an I/O problem.
      • writeText

        public void writeText​(java.lang.String text)
                       throws java.io.IOException
        Writes some text to the character stream.
        Parameters:
        text - the text.
        Throws:
        java.io.IOException - if there is a problem writing to the character stream.
      • writeTextNormalized

        public void writeTextNormalized​(java.lang.String s,
                                        boolean transformNewLine)
                                 throws java.io.IOException
        Writes the given text into the stream using a streaming xml-normalization method.
        Parameters:
        s - the string to be written.
        transformNewLine - whether to encode newlines using character-entities.
        Throws:
        java.io.IOException - if an IO error occured.
      • writeStream

        public void writeStream​(java.io.Reader reader)
                         throws java.io.IOException
        Copies the given reader to the character stream. This method should be used for large chunks of data.
        Parameters:
        reader - the reader providing the text.
        Throws:
        java.io.IOException - if there is a problem writing to the character stream.
      • close

        public void close()
                   throws java.io.IOException
        Closes the underlying character stream.
        Throws:
        java.io.IOException - if there is a problem closing the character stream.
      • writeComment

        public void writeComment​(java.lang.String comment)
                          throws java.io.IOException
        Writes a comment into the generated xml file.
        Parameters:
        comment - the comment text
        Throws:
        java.io.IOException - if there is a problem writing to the character stream.
      • writeNewLine

        public void writeNewLine()
                          throws java.io.IOException
        Writes a linebreak to the writer.
        Throws:
        java.io.IOException - if there is a problem writing to the character stream.
      • flush

        public void flush()
                   throws java.io.IOException
        Flushs the underlying writer.
        Throws:
        java.io.IOException - if something goes wrong.