Class XmlStream


  • public class XmlStream
    extends java.lang.Object
    Class for writing XML in a simplified way.

    Give a writer for it to write the XML directly to. If none is given a StringWriter is used so you can call toString() on the class to get the XML.

    You build XML by calling beginTag(name), addAttribute(id, value), endTag(). Remember to close all your tags, or you'll get an exception when calling toString(). If writing directly to a writer, call isFinalized to verify that all tags have been closed.

    The XML escaping tools only give an interface for escape from and to a string value. Thus writing of all data here is also just available through strings.

    Author:
    Haakon Humberset
    • Constructor Summary

      Constructors 
      Constructor Description
      XmlStream()
      Create an XmlStream writing to a StringWriter.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addAttribute​(java.lang.String key, java.lang.Object value)
      Add a new XML attribute to the last tag started.
      void addContent​(java.lang.String content)
      Add content to the last tag.
      void beginTag​(java.lang.String name)
      Add a new XML tag with the given name.
      void endTag()
      Ends the last tag created.
      boolean isFinalized()
      Check if all tags have been properly closed.
      void setIndent​(java.lang.String indent)
      Set an indent to use for pretty printing of XML.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • XmlStream

        public XmlStream()
        Create an XmlStream writing to a StringWriter. Fetch XML through toString() once you're done creating it.
    • Method Detail

      • setIndent

        public void setIndent​(java.lang.String indent)
        Set an indent to use for pretty printing of XML. Default is no indent.
        Parameters:
        indent - the initial indentation
      • isFinalized

        public boolean isFinalized()
        Check if all tags have been properly closed.
        Returns:
        true if all tags are closed
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • beginTag

        public void beginTag​(java.lang.String name)
        Add a new XML tag with the given name.
        Parameters:
        name - the tag name
      • addAttribute

        public void addAttribute​(java.lang.String key,
                                 java.lang.Object value)
        Add a new XML attribute to the last tag started. The tag cannot already have had content added to it, or been ended. If a null value is added, the attribute will be skipped.
        Parameters:
        key - the attribute name
        value - the attribute value
      • addContent

        public void addContent​(java.lang.String content)
        Add content to the last tag.
        Parameters:
        content - the content to add to the last tag
      • endTag

        public void endTag()
        Ends the last tag created.