All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class org.xmlmiddleware.xmlutils.XMLWriter

java.lang.Object
   |
   +----org.xmlmiddleware.xmlutils.XMLWriter

public class XMLWriter
extends Object
Utility methods for writing an XML document to a Writer.

This is used as the base class for classes such as MapSerializer, which write things out as XML.

If you want to use a specific encoding, the Writer must be an OutputStreamWriter or a subclass of an OutputStreamWriter. For example, you might use the following code to write to the list.xml file with the Shift_JIS encoding:

    // Construct the FileOutputStream.
    OutputStream out = new FileOutputStream("list.xml");
    
// Construct the OutputStreamWriter with the Shift_JIS encoding. This may // throw an UnsupportedEncodingException. Writer writer = new OutputStreamWriter(out, "Shift_JIS");
// Construct the XMLWriter. XMLWriter xmlWriter = new XMLWriter(writer);
// Write to the XMLWriter. ...
// Close the file. writer.close();

If you want to use the default encoding, you can just use a FileWriter. However, no encoding declaration will be written in the XML declaration. For example:

    // Construct a new FileWriter.
    Writer writer = new FileWriter("list.xml");
    
// Construct the XMLWriter. XMLWriter xmlWriter = new XMLWriter(writer);
// Write to the XMLWriter. ...
// Close the file. writer.close();

WARNING: This class does not check that you write a valid XML document. For example, you could write two DOCTYPE statements or improperly nested elements.

Version:
2.0
Author:
Ronald Bourret, 1998-9, 2001

Variable Index

 o attrs
Array for storing attribute names.
 o values
Array for storing attribute values.

Constructor Index

 o XMLWriter()
Construct a new XMLWriter.
 o XMLWriter(Writer)
Construct a new XMLWriter and set the Writer.

Method Index

 o allocateAttrs(int)
Allocate the attrs and values arrays.
 o getWriter()
Get the Writer.
 o setPrettyPrinting(boolean, int)
Set the pretty printing options.
 o setWriter(Writer)
Set the Writer.
 o writeCharacters(char[], int, int)
Write PCDATA starting at a given position
 o writeCharacters(String)
Write PCDATA.
 o writeDOCTYPE(String, String, String)
Write the DOCTYPE statement.
 o writeElementEnd(String)
Write an element end tag.
 o writeElementStart(String, int, boolean)
Write an element start tag.
 o writeXMLDecl()
Write the XML declaration.
 o writeXMLDecl(boolean)
Write the XML declaration with a standalone declaration.

Variables

 o attrs
 public String attrs[]
Array for storing attribute names. Used by subclasses.

 o values
 public String values[]
Array for storing attribute values. Used by subclasses.

Constructors

 o XMLWriter
 public XMLWriter()
Construct a new XMLWriter.

 o XMLWriter
 public XMLWriter(Writer writer)
Construct a new XMLWriter and set the Writer.

Parameters:
writer - The writer. The writer must implement the write(String,int,int) and write(int) methods.

Methods

 o getWriter
 public Writer getWriter()
Get the Writer.

Returns:
The writer.
 o setWriter
 public void setWriter(Writer writer)
Set the Writer.

This method should not be called after you have started writing the document.

Parameters:
writer - The writer. The writer must implement the write(String,int,int) and write(int) methods
 o setPrettyPrinting
 public void setPrettyPrinting(boolean pretty,
                               int increment)
Set the pretty printing options.

This method should not be called after you have started writing the document.

Parameters:
pretty - Whether to perform pretty printing.
increment - The number of spaces by which to indent nested child elements in their parent. If this is less then 0, it is set to 3.
 o writeXMLDecl
 public void writeXMLDecl() throws IOException
Write the XML declaration.

An encoding declaration will be written only if the Writer is an OutputStreamWriter or a subclass of OutputStreamWriter.

 o writeXMLDecl
 public void writeXMLDecl(boolean standalone) throws IOException
Write the XML declaration with a standalone declaration.

An encoding declaration will be written only if the Writer is an OutputStreamWriter or a subclass of OutputStreamWriter.

Parameters:
standalone - Whether the standalone declaration is yes or no.
 o writeDOCTYPE
 public void writeDOCTYPE(String root,
                          String systemID,
                          String publicID) throws IOException
Write the DOCTYPE statement.

Internal subsets are not supported.

Parameters:
root - The root element type.
systemID - The system ID of the external subset. May be null.
publicID - The public ID of the external subset. May be null.
 o writeElementStart
 public void writeElementStart(String name,
                               int numAttrs,
                               boolean empty) throws IOException
Write an element start tag.

Parameters:
name - The name of the element type.
attrs - The number of entries in the attrs and values arrays. May be 0. These entries must be non-null.
empty - Whether the element is empty.
 o writeElementEnd
 public void writeElementEnd(String name) throws IOException
Write an element end tag.

Parameters:
name - The name of the element type.
 o writeCharacters
 public void writeCharacters(String characters) throws IOException
Write PCDATA.

Parameters:
characters - The PCDATA.
 o writeCharacters
 public void writeCharacters(char characters[],
                             int start,
                             int length) throws IOException
Write PCDATA starting at a given position

Parameters:
characters - The PCDATA.
start - The start position.
length - The number of characters to write.
 o allocateAttrs
 public void allocateAttrs(int size)
Allocate the attrs and values arrays. Generally, subclasses allocate arrays large enough to handle the maximum number of attributes found on any element type.

Parameters:
size - The array size.

All Packages  Class Hierarchy  This Package  Previous  Next  Index