|
Nux 1.4 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Using memory consumption close to zero, this interface enables writing arbitrarily
large XML documents onto a destination, such as an OutputStream
;
conceptually similar to the STAX
XMLStreamWriter
interface, except that it is more
XOM friendly, easier to use, and that implementations are required to
guarantee XML wellformedness due to relevant sanity checks.
If a document can be written successfully it can also be reparsed successfully. Thus, wellformedness checks catch roundtrip bugs early, where they are still cheap to fix: At the sender side rather than the (remote) receiver side.
For example, any attempt to write a document containing namespace conflicts,
malformed attribute names or values, multiple or missing root elements, etc.
will throw a WellformednessException
.
Nodes should be written in document order, starting with
writeXMLDeclaration()
, followed by writes for the individual nodes,
finally finishing with writeEndDocument()
. Elements are opened and
closed via writeStartTag(Element)
and writeEndTag()
,
respectively.
Implementations of this interface are retrievable via a
StreamingSerializerFactory
.
Example usage:
StreamingSerializerFactory factory = new StreamingSerializerFactory(); StreamingSerializer ser = factory.createXMLSerializer(System.out, "UTF-8"); // StreamingSerializer ser = factory.createBinaryXMLSerializer(System.out, 0); ser.writeXMLDeclaration(); ser.writeStartTag(new Element("articles")); for (int i = 0; i < 1000000; i++) { Element article = new Element("article"); article.addAttribute(new Attribute("id", String.valueOf(i))); ser.writeStartTag(article); ser.writeStartTag(new Element("prize")); ser.write(new Text(String.valueOf(i * 1000))); ser.writeEndTag(); // close prize ser.writeStartTag(new Element("quantity")); ser.write(new Text("hello world")); ser.writeEndTag(); // close quantity ser.writeEndTag(); // close article } ser.writeEndTag(); // close articles ser.writeEndDocument();
Example usage mixing streaming with convenient writing of entire prefabricated subtrees. For large documents, this approach combines the scalability advantages of streaming with the ease of use of (comparatively small) main-memory subtree construction:
StreamingSerializerFactory factory = new StreamingSerializerFactory(); StreamingSerializer ser = factory.createXMLSerializer(System.out, "UTF-8"); // StreamingSerializer ser = factory.createBinaryXMLSerializer(System.out, 0); ser.writeXMLDeclaration(); ser.writeStartTag(new Element("articles")); for (int i = 0; i < 1000000; i++) { Element article = new Element("article"); article.addAttribute(new Attribute("id", String.valueOf(i))); Element prize = new Element("prize"); prize.appendChild(String.valueOf(i * 1000)); article.appendChild(prize); Element quantity = new Element("quantity"); quantity.appendChild("hello world"); article.appendChild(quantity); ser.write(article); // writes entire subtree } ser.writeEndTag(); // close articles ser.writeEndDocument();
Method Summary | |
void |
flush()
Forces any bytes buffered by the implementation to be written onto the underlying destination, such as an output stream. |
void |
write(Comment comment)
Writes the given comment node. |
void |
write(DocType docType)
Writes the given document type node. |
void |
write(Document doc)
Recursively writes the entire given prefabricated document, including the XML declaration and all its descendants. |
void |
write(Element element)
Recursively writes the entire subtree rooted at the given (potentially parentless) element; this includes attributes and namespaces as if recursively calling writeStartTag/write/writeEndTag for this element and all its descendants, in document order. |
void |
write(ProcessingInstruction instruction)
Writes the given processing instruction node. |
void |
write(Text text)
Writes the given text node. |
void |
writeEndDocument()
Finishes writing the current document, auto-closing any remaining open element tags via writeEndTag calls; Implicitly calls
flush() and releases resources. |
void |
writeEndTag()
Writes the corresponding closing end tag for the element handed to the last writeStartTag call. |
void |
writeStartTag(Element elem)
Writes the start tag for the given (potentially parentless) element; this excludes content and includes attributes and namespaces defined on this element, as if the element had as parent the element handed to the last writeStartTag call. |
void |
writeXMLDeclaration()
Writes the standard XML declaration (including XML version and encoding); must be called before any other write flavour, except
write(Document) . |
Method Detail |
public void flush() throws IOException
IOException
public void writeStartTag(Element elem) throws IOException
writeStartTag
call.
Corresponding closing end tags should be written via
writeEndTag
. A correct program must emit the same number
of writeStartTag
and writeEndTag
calls.
elem
- the element to write a start tag for
IOException
- if the underlying output stream encounters an I/O errorpublic void writeEndTag() throws IOException
writeStartTag
call.
IOException
- if the underlying output stream encounters an I/O errorpublic void write(Document doc) throws IOException
doc
- the document to write
IOException
- if the underlying output stream encounters an I/O errorpublic void write(Element element) throws IOException
For large documents, this method combines the scalability advantages of streaming with the ease of use of (comparatively small) main-memory subtree construction.
element
- the root of the subtree to write
IOException
- if the underlying output stream encounters an I/O errorpublic void write(Text text) throws IOException
text
- the node to write
IOException
- if the underlying output stream encounters an I/O errorpublic void write(Comment comment) throws IOException
comment
- the node to write
IOException
- if the underlying output stream encounters an I/O errorpublic void write(ProcessingInstruction instruction) throws IOException
instruction
- the node to write
IOException
- if the underlying output stream encounters an I/O errorpublic void write(DocType docType) throws IOException
docType
- the node to write
IOException
- if the underlying output stream encounters an I/O errorpublic void writeEndDocument() throws IOException
writeEndTag
calls; Implicitly calls
flush()
and releases resources.
IOException
- if the underlying output stream encounters an I/O errorpublic void writeXMLDeclaration() throws IOException
write
flavour, except
write(Document)
.
IOException
- if the underlying output stream encounters an I/O error
|
Nux 1.4 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |