Class XmlStream

java.lang.Object
com.yahoo.document.serialization.XmlStream

public class XmlStream extends 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 Details

    • XmlStream

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

    • setIndent

      public void setIndent(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 String toString()
      Overrides:
      toString in class Object
    • beginTag

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

      public void addAttribute(String key, 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(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.