## com.lowagie.text.xml
Class XmlWriter

    java.lang.Object
      com.lowagie.text.DocWriter
          com.lowagie.text.xml.XmlWriter

All Implemented Interfaces:
  DocListener, ElementListener, java.util.EventListener

    public class XmlWriter

extends DocWriter
implements DocListener

A `DocWriter` class for XML (Remark: this class is not finished yet!).

An `XmlWriter` can be added as a `DocListener` to a certain `Document` by getting an instance. Every `Element` added to the original `Document` will be written to the `OutputStream` of this `XmlWriter`.

Example:

>      // creation of the document with a certain size and certain margins
>      Document document = new Document(PageSize.A4, 50, 50, 50, 50);
>      try {
>         // this will write XML to the Standard OutputStream
>         XmlWriter.getInstance(document, System.out);
>         // this will write XML to a file called text.html
>         XmlWriter.getInstance(document, new FileOutputStream("text.xml"));
>         // this will write XML to for instance the OutputStream of a HttpServletResponse-object
>         XmlWriter.getInstance(document, response.getOutputStream());
>      }
>      catch(DocumentException de) {
>         System.err.println(de.getMessage());
>      }
>      // this will close the document and all the OutputStreams listening to it
>      document.close();

Field Summary
`static byte[]` `DOCTYPE` This is the reference to the DTD.
`static byte[]` `DTD` This is the place where the DTD is located.
`private java.util.TreeMap` `itext` This is the meta information of the document.
`static byte[]` `PROLOG` This is the first line of the XML page.
`private static java.lang.String[]` `xmlCode` This is an array containing character to XML translations.

Fields inherited from class com.lowagie.text.DocWriter
`closeStream, document, EQUALS, FORWARD, GT, LT, NEWLINE, open, os, pageSize, pause, QUOTE, SPACE, TAB`

Constructor Summary
`protected` `XmlWriter(Document doc, java.io.OutputStream os)` Constructs an `XmlWriter`.
`protected` `XmlWriter(Document doc, java.io.OutputStream os, java.lang.String dtd)` Constructs an `XmlWriter`.

Method Summary
`boolean` `add(Element element)` Signals that an `Element` was added to the `Document`.
`(package private) static void` `addTabs(java.lang.StringBuffer buf, int indent)` Adds a number of tabs to a `StringBuffer`.
`void` `close()` Signals that the `Document` was closed and that no other `Elements` will be added.
`(package private) static java.lang.String` `encode(java.lang.String string, int indent)` Encodes a `String`.
`static XmlWriter` `getInstance(Document document, java.io.OutputStream os)` Gets an instance of the `XmlWriter`.
`static XmlWriter` `getInstance(Document document, java.io.OutputStream os, java.lang.String dtd)` Gets an instance of the `XmlWriter`.
`boolean` `newPage()` Signals that an new page has to be LTed.
`void` `open()` Signals that the `Document` has been opened and that `Elements` can be added.
`private void` `write(Element element, int indent)` Writes the XML representation of an element.
`private void` `write(Font font)` Writes the XML representation of a `Font`.
`private void` `write(Rectangle rectangle)` Writes the XML representation of this `Rectangle`.
`private void` `writeSection(Section section, int indent)` Writes the XML representation of a section.

Methods inherited from class com.lowagie.text.DocWriter
`add, addTabs, clearTextWrap, flush, getISOBytes, hasMarkupAttributes, isCloseStream, pause, removeWatermark, resetFooter, resetHeader, resetPageCount, resume, setCloseStream, setFooter, setHeader, setMarginMirroring, setMargins, setPageCount, setPageSize, write, write, writeEnd, writeEnd, writeMarkupAttributes, writeStart`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Methods inherited from interface com.lowagie.text.DocListener
`add, clearTextWrap, removeWatermark, resetFooter, resetHeader, resetPageCount, setFooter, setHeader, setMarginMirroring, setMargins, setPageCount, setPageSize`

Field Detail

### PROLOG

    public static final byte[] PROLOG

  This is the first line of the XML page.

### DOCTYPE

    public static final byte[] DOCTYPE

  This is the reference to the DTD.

### DTD

    public static final byte[] DTD

  This is the place where the DTD is located.

### xmlCode

    private static final java.lang.String[] xmlCode

  This is an array containing character to XML translations.

### itext

    private java.util.TreeMap itext

  This is the meta information of the document.

Constructor Detail

### XmlWriter

    protected XmlWriter(Document doc,
                        java.io.OutputStream os)

  Constructs an `XmlWriter`.

  Parameters:
    `doc` - The `Document` that has to be written as XML
    `os` - The `OutputStream` the writer has to write to.

### XmlWriter

    protected XmlWriter(Document doc,
                        java.io.OutputStream os,
                        java.lang.String dtd)

  Constructs an `XmlWriter`.

  Parameters:
    `doc` - The `Document` that has to be written as XML
    `os` - The `OutputStream` the writer has to write to.
    `dtd` - The DTD to use

Method Detail

### getInstance

    public static XmlWriter getInstance(Document document,
                                        java.io.OutputStream os)

  Gets an instance of the `XmlWriter`.

  Parameters:
    `document` - The `Document` that has to be written
    `os` - The `OutputStream` the writer has to write to.
  Returns:
    a new `XmlWriter`

### getInstance

    public static XmlWriter getInstance(Document document,
                                        java.io.OutputStream os,
                                        java.lang.String dtd)

  Gets an instance of the `XmlWriter`.

  Parameters:
    `document` - The `Document` that has to be written
    `os` - The `OutputStream` the writer has to write to.
    `dtd` - The DTD to use
  Returns:
    a new `XmlWriter`

### add

    public boolean add(Element element)
                throws DocumentException

  Signals that an `Element` was added to the `Document`.

  Specified by:
    `add` in interface `ElementListener`
  Overrides:
    `add` in class `DocWriter`
  Parameters:
    `element` - A high level object that will be added to the XML
  Returns:
    `true` if the element was added, `false` if not.
  Throws:
    `DocumentException` - when a document isn't open yet, or has been closed

### open

    public void open()

  Signals that the `Document` has been opened and that `Elements` can be added.

  Specified by:
    `open` in interface `DocListener`
  Overrides:
    `open` in class `DocWriter`

### newPage

    public boolean newPage()
                    throws DocumentException

  Signals that an new page has to be LTed.

  Specified by:
    `newPage` in interface `DocListener`
  Overrides:
    `newPage` in class `DocWriter`
  Returns:
    `true` if the page was added, `false` if not.
  Throws:
    `DocumentException` - when a document isn't open yet, or has been closed

### close

    public void close()

  Signals that the `Document` was closed and that no other `Elements` will be added.

  Specified by:
    `close` in interface `DocListener`
  Overrides:
    `close` in class `DocWriter`

### write

    private void write(Element element,
                       int indent)
                throws java.io.IOException

  Writes the XML representation of an element.

  Parameters:
    `element` - the element
    `indent` - the indentation
  Throws:
    `java.io.IOException`

### writeSection

    private void writeSection(Section section,
                              int indent)
                       throws java.io.IOException

  Writes the XML representation of a section.

  Parameters:
    `section` - the section to write
    `indent` - the indentation
  Throws:
    `java.io.IOException`

### write

    private void write(Rectangle rectangle)
                throws java.io.IOException

  Writes the XML representation of this `Rectangle`.

  Parameters:
    `rectangle` - a `Rectangle`
  Throws:
    `java.io.IOException`

### encode

    static final java.lang.String encode(java.lang.String string,
                                         int indent)

  Encodes a `String`.

  Parameters:
    `string` - the `String` to encode
    `indent` - counter that keeps the number of tabs that has to be added for indentation
  Returns:
    the encoded `String`

### addTabs

    static final void addTabs(java.lang.StringBuffer buf,
                              int indent)

  Adds a number of tabs to a `StringBuffer`.

  Parameters:
    `buf` - the stringbuffer
    `indent` - the number of tabs to add

### write

    private void write(Font font)
                throws java.io.IOException

  Writes the XML representation of a `Font`.

  Parameters:
    `font` - a `Font`
  Throws:
    `java.io.IOException`