Class XmlWriter


  • public class XmlWriter
    extends Object
    A writer of XML output streams.

    An XML writer knows hardly anything about XML document well-formedness, to say nothing of validity. It relies upon the invoker to ensure that the generated document is well-formed and, if required, valid.

    Note: This class is incomplete. In the next draft it will be extended to more closely mimic the Xerces XMLScanner, and to support output to both SAX streams and DOM documents.

    Author:
    JAX-RPC Development Team
    • Constructor Detail

      • XmlWriter

        public XmlWriter​(OutputStream out,
                         String enc,
                         boolean declare)
                  throws UnsupportedEncodingException,
                         IOException
        Creates a new writer that will write to the given byte-output stream using the given encoding. An initial XML declaration will optionally be written to the stream.

        Parameters:
        out - The target byte-output stream
        enc - The character encoding to be used
        declare - If true, write the XML declaration to the output stream
        Throws:
        IOException - If an I/O error occurs
        UnsupportedEncodingException - If the named encoding is not supported
      • XmlWriter

        public XmlWriter​(OutputStream out)
                  throws IOException
        Creates a new writer that will write to the given byte-output stream using the UTF-8 encoding. An initial XML declaration will be written to the stream.

        Parameters:
        out - The target byte-output stream
        Throws:
        IOException - If an I/O error occurs
    • Method Detail

      • setQuote

        public void setQuote​(char quote)
        Sets the quote character to be used by this writer when writing attribute values.

        Parameters:
        quote - The new quote character, either a QUOTATION MARK ('\u0022'), or an APOSTROPHE-QUOTE ('\u0027')
        Throws:
        IllegalArgumentException - If the argument is neither of the above characters
      • doctype

        public void doctype​(String root,
                            String dtd)
                     throws IOException
        Writes a DOCTYPE declaration.

        Parameters:
        root - The name of the root element
        dtd - The URI of the document-type definition
        Throws:
        IOException - If an I/O error occurs
      • start

        public void start​(String name)
                   throws IOException
        Writes a start tag for the named element.

        Parameters:
        name - The name to be used in the start tag
        Throws:
        IOException - If an I/O error occurs
      • attribute

        public void attribute​(String name,
                              String value)
                       throws IOException
        Writes an attribute for the current element.

        Parameters:
        name - The attribute's name
        value - The attribute's value
        Throws:
        IllegalStateException - If the previous method invoked upon this object was neither start nor attribute
        IOException - If an I/O error occurs
      • attributeUnquoted

        public void attributeUnquoted​(String name,
                                      String value)
                               throws IOException
        Writes an attribute (unquoted) for the current element.

        Parameters:
        name - The attribute's name
        value - The attribute's value
        Throws:
        IllegalStateException - If the previous method invoked upon this object was neither start nor attribute
        IOException - If an I/O error occurs
      • attribute

        public void attribute​(String prefix,
                              String name,
                              String value)
                       throws IOException
        Writes an attribute for the current element.

        Parameters:
        prefix - The attribute's prefix
        name - The attribute's name
        value - The attribute's value
        Throws:
        IllegalStateException - If the previous method invoked upon this object was neither start nor attribute
        IOException - If an I/O error occurs
      • attributeUnquoted

        public void attributeUnquoted​(String prefix,
                                      String name,
                                      String value)
                               throws IOException
        Writes an attribute (unquoted) for the current element.

        Parameters:
        prefix - The attribute's prefix
        name - The attribute's name
        value - The attribute's value
        Throws:
        IllegalStateException - If the previous method invoked upon this object was neither start nor attribute
        IOException - If an I/O error occurs
      • attributeName

        public void attributeName​(String prefix,
                                  String name)
                           throws IOException
        Writes an attribute name for the current element. After invoking this method, invoke the attributeValue method to write the attribute value, or invoke the attributeValueToken method to write one or more space-separated value tokens.

        Parameters:
        prefix - The attribute's prefix
        name - The attribute's name
        Throws:
        IllegalStateException - If the previous method invoked upon this object was neither start nor attribute
        IOException
      • end

        public void end​(String name)
                 throws IOException
        Writes an end tag for the named element.

        Parameters:
        name - The name to be used in the end tag
        Throws:
        IOException - If an I/O error occurs
      • chars

        public void chars​(String chars)
                   throws IOException
        Writes some character data.

        Parameters:
        chars - The character data to be written
        Throws:
        IOException - If an I/O error occurs
      • charsUnquoted

        public void charsUnquoted​(String chars)
                           throws IOException
        Writes some character data, skipping quoting.

        Parameters:
        chars - The character data to be written
        Throws:
        IOException - If an I/O error occurs
      • charsUnquoted

        public void charsUnquoted​(char[] buf,
                                  int off,
                                  int len)
                           throws IOException
        Writes some character data, skipping quoting.

        Parameters:
        buf - Buffer containing the character data to be written
        off - The offset of the data to be written
        len - The length of the data to be written
        Throws:
        IOException - If an I/O error occurs
      • leaf

        public void leaf​(String name,
                         String chars)
                  throws IOException
        Writes a leaf element with the given character content.

        Parameters:
        name - The name to be used in the start and end tags
        chars - The character data to be written

        This method writes a start tag with the given name, followed by the given character data, followed by an end tag. If the chars parameter is null or the empty string then an empty tag is written.

        Throws:
        IOException - If an I/O error occurs
      • leaf

        public void leaf​(String name)
                  throws IOException
        Writes an empty leaf element.

        Parameters:
        name - The name to be used in the empty-element tag
        Throws:
        IOException
      • flush

        public void flush()
                   throws IOException
        Flushes the writer.

        Throws:
        IOException - If an I/O error occurs
      • close

        public void close()
                   throws IOException
        Flushes the writer and closes the underlying byte-output stream.

        Throws:
        IOException - If an I/O error occurs