Class XMLWriter


  • public class XMLWriter
    extends Object
    An XML writer provides convenient methods to produce XML code.
    • Constructor Detail

      • XMLWriter

        public XMLWriter​(OutputStream out)
                  throws IOException
        Constructs a new XML writer that will write XML code to the specified output stream in UTF-8 encoding. This constructor will immediately write the XML prolog.
        Parameters:
        out - the output stream
        Throws:
        IOException - if an error occurs while initialising the XML writer
      • XMLWriter

        public XMLWriter​(Writer writer)
                  throws IOException
        Constructs a new XML writer that will write XML code to the specified writer. If the code is encoded some time, it should be encoded in UTF-8. This constructor will immediately write the XML prolog.
        Parameters:
        writer - the writer
        Throws:
        IOException - if an error occurs while initialising the XML writer
    • Method Detail

      • close

        public void close()
        Closes this XML writer.
      • writeStartElement

        public void writeStartElement​(String name)
                               throws IOException
        Writes the start of an element.
        Parameters:
        name - the element name
        Throws:
        IOException - if a writing error occurs
      • writeAttribute

        public void writeAttribute​(String name,
                                   String value)
                            throws IOException
        Writes an attribute of an element. This can only be called after writeStartElement(). Any special characters in the attribute value will be escaped with XML entities.
        Parameters:
        name - the attribute name
        value - the attribute value
        Throws:
        IOException - if the writer is not in the start of an element, or if a writing error occurs
      • writeEndElement

        public void writeEndElement()
                             throws IOException
        Writes the end of an element. This can only be called if writeStartElement() was called earlier. Eventually it should be called once for each call to writeStartElement().
        Throws:
        IOException - if no matching start was found, or if a writing error occurs
      • writeCharacters

        public void writeCharacters​(String s)
                             throws IOException
        Writes characters. Any special characters in the string will be escaped with XML entities.
        Parameters:
        s - the characters
        Throws:
        IOException - if a writing error occurs
      • writeElement

        public void writeElement​(String xml)
                          throws IOException
        Writes a complete XML element. It parses the XML code into an element and then calls writeElement(Element).
        Parameters:
        xml - the XML code for the element
        Throws:
        IOException - if the XML code is not a valid element or a writing error occurs
      • writeElement

        public void writeElement​(Element elem)
                          throws IOException
        Writes a complete XML element.
        Parameters:
        elem - the element
        Throws:
        IOException - if a writing error occurs
      • escapeString

        public static String escapeString​(String s)
        Escapes any special XML characters with XML entities.
        Parameters:
        s - the string
        Returns:
        the escaped string