Class XMLWriter


  • public class XMLWriter
    extends Object
    Helper class to write XML documents.

    IMPORTANT: flush() or close() must always be called to assure all content is flushed to the underlying streams.

    If a StringWriter is used the flush() or close() must be be called before calling the toString() method

    • Constructor Detail

      • XMLWriter

        public XMLWriter​(String path)
        Creates a new XML document on disk.
        Parameters:
        path - the full path of the document to create. If any directory in the path does not exists yet it will be created automatically. The document is always written in UTF-8 and the type of line-breaks is the one of the platform where the application runs.
      • XMLWriter

        public XMLWriter​(Writer writer)
        Creates a new XML document for a given writer object.
        Parameters:
        writer - the writer to use to output the document. If this writer outputs to bytes it must be set to output in UTF-8.
    • Method Detail

      • setLineBreak

        public void setLineBreak​(String lineBreak)
        Sets the type of line-break to use when writing out the document. By default, the type is the one of the platform. This method can be used for example, to force the line-breaks to be "\n" instead of "\r\n" when writing out on a string that will be later written out on a file where "\n" will be converted to "\r\n" (therefore avoiding to end up with "\r\r\n").
        Parameters:
        lineBreak - the new type of line-break to use.
      • getLineBreak

        public String getLineBreak()
        Gets the line break string currently used for this writer.
        Returns:
        the line break string used.
      • close

        public void close()
        Closes the writer and release any associated resources.
      • writeStartDocument

        public void writeStartDocument()
        Writes the start of the document. This method generates the XML declaration.
      • writeStartHTMLDocument

        public void writeStartHTMLDocument​(String title)
        Writes the start of a XHTML document, including the head and start of body element.
        Parameters:
        title - the title of the document (can be null).
      • writeEndDocument

        public void writeEndDocument()
        Writes the end of the document. This method closes any open tag, and flush the writer.
      • writeStartElement

        public void writeStartElement​(String name)
        Writes the start of an element.
        Parameters:
        name - the name of the element to start.
      • writeEndElement

        public void writeEndElement()
        Writes the end of the last started element.
      • writeEndElementLineBreak

        public void writeEndElementLineBreak()
        Writes the end of the last started element and writes a line-break.
      • writeElementString

        public void writeElementString​(String name,
                                       String content)
        Writes an element and its content.
        Parameters:
        name - the name of the element to write.
        content - the content to enclose inside this element.
      • writeAttributeString

        public void writeAttributeString​(String name,
                                         String value)
        Writes an attribute and its associated value. You must use writeStartElement(String) just before.
        Parameters:
        name - the name of the attribute.
        value - the value of the attribute.
      • writeString

        public void writeString​(String text)
        Writes a string. The text is automatically escaped.
        Parameters:
        text - the text to output.
      • writeRawXML

        public void writeRawXML​(String xmlData)
        Writes a chunk of raw XML (and line-breaks are normalized to platform specific ones). If a tag is open, it is closed automatically before the data are written. Use appendRawXML(String) to output without preliminary closing.
        Parameters:
        xmlData - the data to output. No escaping is performed, but the line-breaks are converted to the line-break type of the output.
      • appendRawXML

        public void appendRawXML​(String xmlData)
        Writes a chunk of raw XML (and line-breaks are normalized to platform specific ones). If a tag is open it is not closed (so this allows you to output raw attributes).
        Parameters:
        xmlData - the data to output. No escaping is performed, but the line-breaks are converted to the line-break type of the output.
      • writeComment

        public void writeComment​(String text,
                                 boolean withLineBreak)
        Writes a comment.
        Parameters:
        text - the text of the comment.
        withLineBreak - add a line break at the end of the comment.
      • writeLineBreak

        public void writeLineBreak()
        Writes a line-break, and if the writer is in a start tag, close it before.
      • flush

        public void flush()
        Flushes the current buffer to the device.