|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Usage
Implementations of this interface provide the capability of writing XML documents. They should provide at least one constructor or method that specifies an underlying output stream for the XML output.
import tribble.xml.*; public class MyXmlOutputStream implements tribble.xml.XmlOutputStreamI { public MyXmlOutputStream(java.io.OutputStream out) ...; ... }
Example
We want to generate the following XML document:
<?xml version='1.0' stand-alone='yes'?> <emp:employee type='perm' ssn='647-37-0999'> <emp:name>John Q. Smith</emp:name> <emp:address> <emp:street>3817 Newhaven Dr.</emp:street> <emp:city>Dallas</emp:city> <emp:state>TX</emp:state> <emp:zip>75022</emp:zip> </emp:address> <emp:hire when='2001-02-11'/> <emp:dept>Customer Support A</emp:dept> </emp:employee>
First we create an XML output stream and initialize it:
XmlOutputStreamI xml; Properties props; xml = new MyXmlOutputStream(...); props = ...; xml.initialize(props);
Next we specify that we want an <?xml> directive node in the output with a stand-alone='yes' attribute:
xml.startDirective("xml"); xml.addAttribute("stand-alone", "yes"); xml.endDirective();
Next we start the outermost <emp:employee> node, and set its attributes:
xml.startTag("emp", "employee"); xml.addAttribute("type", "perm"); xml.addAttribute("ssn", "647-37-0999");
For convenience, we can set the namespace prefix for all of the remaining
nodes, since they are all the same (emp:), and use the simpler form
of the startTag()
method:
xml.setNamespace("emp");
Next we add the <emp:name> node and its text content:
xml.startTag("name"); xml.addText("John Q. Smith"); xml.endTag();
Likewise, we continue in a similar fashion for the remaining XML nodes:
xml.startTag("address"); xml.startTag("street"); xml.addText("3817 Newhaven Dr."); xml.endTag(); xml.startTag("city"); xml.addText("Dallas"); xml.endTag(); xml.startTag("state"); xml.addText("TX"); xml.endTag(); xml.startTag("zip"); xml.addText("75022"); xml.endTag(); xml.endTag(); // Close the <emp:address> node xml.startTag("hire"); xml.addAttribute("when", "2001-02-11"); xml.endTag(); xml.startTag("dept"); xml.addText("Customer Support A"); xml.endTag();
Next we terminate the outermost XML node (emp:employee):
xml.endTag(); // Close the <emp:employee> node
Finally we flush any pending output for the output stream and then close it:
xml.flush(); xml.close();
XmlReaderI
Field Summary | |
static java.lang.String |
REV
Revision information. |
static int |
SERIES
API series number. |
Method Summary | |
void |
addAttribute(java.lang.String attr,
java.lang.String val)
Add an attribute value to the current XML element. |
void |
addCharacter(int ch)
Append a character to the text content of the current XML element. |
void |
addText(java.lang.String text)
Append text content to the current XML element. |
void |
close()
Close this XML output stream. |
void |
endDirective()
Terminate the current directive node for this XML output stream. |
void |
endTag()
Terminate the current element node for this XML output stream. |
void |
flush()
Flush any pending output to this XML output stream. |
void |
initialize(java.util.Hashtable parms)
Initialize this XML output stream. |
void |
setNamespace(java.lang.String namesp)
Establish the namespace prefix for XML element nodes written to this output stream. |
void |
startDirective(java.lang.String name)
Write a directive node to this XML output stream. |
void |
startTag(java.lang.String tag)
Write an element node to this XML output stream. |
void |
startTag(java.lang.String namesp,
java.lang.String tag)
Write an element node to this XML output stream. |
Field Detail |
public static final java.lang.String REV
public static final int SERIES
Method Detail |
public void initialize(java.util.Hashtable parms) throws java.io.IOException
This must be the first method to be called on this object, prior to adding any element nodes or attributes.
The implementation of this interface may allow this method to be called more than once.
parms
- A hash table containing name/value pairs with which to initialize this
XML output stream. This argument may be allowed to be null, depending on
the implementation of this interface.
java.io.IOException
- Thrown if the specified hash table entries are malformed, or if some other
error occurs.close()
public void flush() throws java.io.IOException
java.io.IOException
- Thrown if an I/O (output) error occurs.close()
public void close() throws java.io.IOException
This must be the last method to be called on this XML output object.
Implementations of this method typically should call the
flush()
method.
java.io.IOException
- Thrown if an I/O (output) error occurs.initialize()
,
flush()
public void setNamespace(java.lang.String namesp) throws java.io.IOException
This setting stays in force until a subsequent call to this method changes it.
namesp
- The default namespace prefix for subsequent XML elements. Note that the
':' separator should not be included. This argument can
be empty ("") or null, in which case there is no default
namespace prefix.
java.io.IOException
- Thrown if an I/O (write) error occurs.public void startDirective(java.lang.String name) throws java.io.IOException
For example, the call
startDirective("xml");writes the following to the output stream:
<?xml>
Attributes can be added to a directive using the
addAttribute()
method.
The directive must be terminated by calling the
endDirective()
method.
name
- A directive node name.
For example, a name of "perms" causes an XML directive element of
<?perms?> to be written.
java.io.IOException
- Thrown if an I/O (write) error occurs.endDirective()
,
addAttribute()
public void endDirective() throws java.io.IOException
This call closes the directive node opened by a previous call to
startDirective()
.
java.io.IOException
- Thrown if an I/O (write) error occurs.startDirective()
public void startTag(java.lang.String namesp, java.lang.String tag) throws java.io.IOException
The new element will be nested within the previously unterminated element, if there is one.
For example, the call
startTag("ns", "tag");writes the following to the output stream:
<ns:tag>
The XML element must be terminated by calling the
endTag()
method.
namesp
- The namespace prefix of the element, without the ':' separator.
This argument can be empty ("") or null, in which case the node
has no namespace prefix.tag
- The name of the element. For example, a name of "ssn" and a
namespace of "emp" will be written to the XML output stream as
<emp:ssn>.
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
,
endTag()
,
addAttribute()
public void startTag(java.lang.String tag) throws java.io.IOException
The new element will be nested within the previously unterminated element, if there is one. The new element will use the default namespace prefix, if one has been established.
For example, the call
startTag("tag");writes the following to the output stream:
<ns:tag>
The XML element must be terminated by calling the
endTag()
method.
tag
- The name of the element. For example, a name of "ssn" and a
default namespace of "emp" will be written to the XML output
stream as <emp:ssn>.
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
,
setNamespace()
,
endTag()
,
addAttribute()
public void endTag() throws java.io.IOException
This call closes the element opened by a previous call to
startTag()
.
For example, the call
endTag();writes the following to the output stream:
</ns:tag>
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
,
startTag()
public void addAttribute(java.lang.String attr, java.lang.String val) throws java.io.IOException
For example, the call
addAttribute("size", "80");writes the following to the output stream:
size="80"
attr
- An attribute name.val
- The text value of the attribute.
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
public void addText(java.lang.String text) throws java.io.IOException
This method may be called multiple times, each call appending additional text characters to the content of the current XML element.
Unprintable characters (e.g., control characters) and special characters
(e.g., angle brackets '<' and '>') are written to
the output stream as XML character entities (e.g.,  or
<).
Unprintable characters can also be written explicitly using the
addCharacter()
method.
Each newline sequence (CR \u000D, LF \u000A, or a CR LF pair) is written as a single canonical newline, according to the conventions of the underlying output stream.
Note that this method can be implemented to call the
addCharacter()
method for each character in
text.
text
- Text characters to append to the content of the current XML element.
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
,
addCharacter()
public void addCharacter(int ch) throws java.io.IOException
This method may be called multiple times, each call appending additional text characters to the content of the current XML element.
Printable characters are written as is to the XML output stream, according to the character encoding of the underlying output stream. Unprintable characters (e.g., control characters) are written as character entities (&#dd;).
A newline sequence (CR \u000D, LF \u000A, or a CR LF pair) is written as a single canonical newline, according to the conventions of the underlying output stream.
ch
- A text character code to append to the content of the current XML element.
java.io.IOException
- Thrown if an I/O (write) error occurs.startTag()
,
addText()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |