# Class XMLEncoder

java.lang.Object
org.apache.cayenne.util.XMLEncoder

public class XMLEncoder extends Object

A helper class to encode objects to XML.

Usage:

         XMLEncoder encoder = new XMLEncoder(writer);
         encoder
             .start("tag").attribute("name", "tag_name_attribute")
             .start("nested_tag").attribute("name", "nested_tag_name).cdata("tag text element").end()
             .end();

Since:
  1.1, 4.1 API is greatly reworked to be more usable

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected boolean`
  `cdata`
  `protected int`
  `currentTagLevel`
  `protected String`
  `indent`
  `protected boolean`
  `indentLine`
  `protected int`
  `indentTimes`
  `protected int`
  `lastTagLevel`
  `protected Deque<String>`
  `openTags`
  `protected PrintWriter`
  `out`
  `protected String`
  `projectVersion`
  `protected boolean`
  `tagOpened`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `XMLEncoder(PrintWriter out)`
  `XMLEncoder(PrintWriter out, String indent)`
  `XMLEncoder(PrintWriter out, String indent, String projectVersion)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `XMLEncoder`
  `attribute(String name, boolean value)`
  `XMLEncoder`
  `attribute(String name, int value)`
  `XMLEncoder`
  `attribute(String name, String value)`
  `XMLEncoder`
  `attribute(String name, String value, boolean newLine)`
  `XMLEncoder`
  `cdata(String data)`
  `XMLEncoder`
  `cdata(String data, boolean escape)`
  `XMLEncoder`
  `end()`
  This method will track presence of nested tags and print closure accordingly
  `XMLEncoder`
  `indent(int i)`
  `XMLEncoder`
  `nested(Collection<? extends XMLSerializable> collection, ConfigurationNodeVisitor delegate)`
  `XMLEncoder`
  `nested(Map<?, ? extends XMLSerializable> map, ConfigurationNodeVisitor delegate)`
  `XMLEncoder`
  `nested(XMLSerializable object, ConfigurationNodeVisitor delegate)`
  `XMLEncoder`
  `print(String text)`
  `XMLEncoder`
  `println()`
  `XMLEncoder`
  `println(String text)`
  `XMLEncoder`
  `projectVersion()`
  Inserts an optional project version attribute in the output.
  `XMLEncoder`
  `property(String name, boolean b)`
  Prints a common XML element - property with name and value.
  `XMLEncoder`
  `property(String name, int i)`
  Prints a common XML element - property with name and value.
  `XMLEncoder`
  `property(String name, String value)`
  Prints a common XML element - property with name and value.
  `XMLEncoder`
  `simpleTag(String tag, String value)`
  Prints common XML element - tag with name and text value (<tag>value</tag>) If value is empty, nothing will be printed.
  `XMLEncoder`
  `start(String tag)`

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### projectVersion

    protected String projectVersion

  - ### indent

    protected String indent

  - ### out

    protected PrintWriter out

  - ### indentLine

    protected boolean indentLine

  - ### indentTimes

    protected int indentTimes

  - ### tagOpened

    protected boolean tagOpened

  - ### cdata

    protected boolean cdata

  - ### currentTagLevel

    protected int currentTagLevel

  - ### lastTagLevel

    protected int lastTagLevel

  - ### openTags

    protected Deque<String> openTags

- ## Constructor Details

  - ### XMLEncoder

    public XMLEncoder(PrintWriter out)

  - ### XMLEncoder

    public XMLEncoder(PrintWriter out, String indent)

  - ### XMLEncoder

    public XMLEncoder(PrintWriter out, String indent, String projectVersion)
    Since:
      3.1

- ## Method Details

  - ### indent

    public XMLEncoder indent(int i)

  - ### print

    public XMLEncoder print(String text)

  - ### println

    public XMLEncoder println(String text)

  - ### println

    public XMLEncoder println()
    Since:
      3.1

  - ### start

    public XMLEncoder start(String tag)
    Parameters:
      `tag` - to start
    Returns:
      this
    Since:
      4.1

  - ### end

    public XMLEncoder end()
    This method will track presence of nested tags and print closure accordingly
    Returns:
      this
    Since:
      4.1

  - ### attribute

    public XMLEncoder attribute(String name, String value)
    Parameters:
      `name` - of the attribute
      `value` - of the attribute
    Returns:
      this
    Since:
      4.1

  - ### attribute

    public XMLEncoder attribute(String name, String value, boolean newLine)
    Parameters:
      `name` - of the attribute
      `value` - of the attribute
      `newLine` - should this attribute be printed on new line
    Returns:
      this
    Since:
      4.1

  - ### attribute

    public XMLEncoder attribute(String name, boolean value)
    Parameters:
      `name` - of the attribute
      `value` - of the attribute
    Returns:
      this
    Since:
      4.1

  - ### attribute

    public XMLEncoder attribute(String name, int value)
    Parameters:
      `name` - of the attribute
      `value` - of the attribute
    Returns:
      this
    Since:
      4.1

  - ### cdata

    public XMLEncoder cdata(String data)
    Parameters:
      `data` - char data
    Returns:
      this
    Since:
      4.1

  - ### cdata

    public XMLEncoder cdata(String data, boolean escape)
    Parameters:
      `data` - char data
      `escape` - does this data need to be enclosed into <![CDATA[ ... ]]>
    Returns:
      this
    Since:
      4.1

  - ### nested

    public XMLEncoder nested(XMLSerializable object, ConfigurationNodeVisitor delegate)
    Parameters:
      `object` - nested object to serialize
      `delegate` - visitor
    Returns:
      this
    Since:
      4.1

  - ### nested

    public XMLEncoder nested(Collection<? extends XMLSerializable> collection, ConfigurationNodeVisitor delegate)
    Parameters:
      `collection` - of nested objects
      `delegate` - visitor
    Returns:
      this
    Since:
      4.1

  - ### nested

    public XMLEncoder nested(Map<?, ? extends XMLSerializable> map, ConfigurationNodeVisitor delegate)
    Parameters:
      `map` - of nested objects
      `delegate` - visitor
    Returns:
      this
    Since:
      4.1

  - ### property

    public XMLEncoder property(String name, String value)
    Prints a common XML element - property with name and value.
    Since:
      4.1

  - ### property

    public XMLEncoder property(String name, boolean b)
    Prints a common XML element - property with name and value.
    Since:
      4.1

  - ### property

    public XMLEncoder property(String name, int i)
    Prints a common XML element - property with name and value.
    Since:
      4.1

  - ### simpleTag

    public XMLEncoder simpleTag(String tag, String value)
    Prints common XML element - tag with name and text value (<tag>value</tag>) If value is empty, nothing will be printed.
    Since:
      4.1

  - ### projectVersion

    public XMLEncoder projectVersion()
    Inserts an optional project version attribute in the output. If the project version is not initialized for encoder, will do nothing.
    Since:
      4.1