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
-
attrs
- Array for storing attribute names.
-
values
- Array for storing attribute values.
-
XMLWriter()
- Construct a new XMLWriter.
-
XMLWriter(Writer)
- Construct a new XMLWriter and set the Writer.
-
allocateAttrs(int)
- Allocate the attrs and values arrays.
-
getWriter()
- Get the Writer.
-
setPrettyPrinting(boolean, int)
- Set the pretty printing options.
-
setWriter(Writer)
- Set the Writer.
-
writeCharacters(char[], int, int)
- Write PCDATA starting at a given position
-
writeCharacters(String)
- Write PCDATA.
-
writeDOCTYPE(String, String, String)
- Write the DOCTYPE statement.
-
writeElementEnd(String)
- Write an element end tag.
-
writeElementStart(String, int, boolean)
- Write an element start tag.
-
writeXMLDecl()
- Write the XML declaration.
-
writeXMLDecl(boolean)
- Write the XML declaration with a standalone declaration.
attrs
public String attrs[]
- Array for storing attribute names. Used by subclasses.
values
public String values[]
- Array for storing attribute values. Used by subclasses.
XMLWriter
public XMLWriter()
- Construct a new XMLWriter.
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.
getWriter
public Writer getWriter()
- Get the Writer.
- Returns:
- The writer.
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
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.
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.
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.
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.
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.
writeElementEnd
public void writeElementEnd(String name) throws IOException
- Write an element end tag.
- Parameters:
- name - The name of the element type.
writeCharacters
public void writeCharacters(String characters) throws IOException
- Write PCDATA.
- Parameters:
- characters - The PCDATA.
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.
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