Top Description Inners Fields Constructors Methods
org.dom4j.io

public Class STAXEventWriter

extends Object
Class Inheritance
Imports
java.io.File, .FileWriter, .IOException, .OutputStream, .StringWriter, .Writer, java.util.Iterator, javax.xml.namespace.QName, javax.xml.stream.XMLEventFactory, .XMLOutputFactory, .XMLStreamException, javax.xml.stream.events.Characters, .DTD, .EndDocument, .EndElement, .EntityReference, .ProcessingInstruction, .StartDocument, .StartElement, javax.xml.stream.util.XMLEventConsumer, org.dom4j.Attribute, .Branch, .CDATA, .Comment, .Document, .DocumentType, .Element, .Entity, .Namespace, .Node, .Text

Writes DOM4J Nodes to a StAX event stream. In addition the createXXX methods are provided to directly create STAX events from DOM4J nodes.
Author
Christian Niles

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
STAXEventWriter.AttributeIterator

Internal Iteratorimplementation used to pass DOM4J Attributes to the stream.

private class
STAXEventWriter.NamespaceIterator

Internal Iteratorimplementation used to pass DOM4J Namespaces to the stream.

Field Summary

Modifier and TypeField and Description
private XMLEventConsumer
consumer

The event stream to which events are written.

private XMLEventFactory
factory

The event factory used to construct events.

private XMLOutputFactory

Constructor Summary

AccessConstructor and Description
public
public
STAXEventWriter(File
The file to which events will be written.
file
)

Constructs a STAXEventWriter that writes events to the provided file.

public
STAXEventWriter(Writer
The character stream to which events will be written.
writer
)

Constructs a STAXEventWriter that writes events to the provided character stream.

public
STAXEventWriter(OutputStream
The output stream to which events will be written.
stream
)

Constructs a STAXEventWriter that writes events to the provided stream.

public
STAXEventWriter(XMLEventConsumer
The event stream to which events will be written.
consumer
)

Constructs a STAXEventWriter that writes events to the provided event stream.

Method Summary

Modifier and TypeMethod and Description
public Attribute

Returns:

The newly constructed javax.xml.stream.events.Attribute event.
createAttribute
(Attribute
The Attributefrom which to construct the event.
attr
)

Constructs a STAX javax.xml.stream.events.Attributeevent from a DOM4J Attribute.

public Characters

Returns:

The constructed Charactersevent.
createCharacters
(Text
The Textfrom which to construct the event.
text
)

Constructs a STAX Charactersevent from a DOM4J Text.

public Characters

Returns:

The newly constructed Charactersevent.
createCharacters
(CDATA
The CDATAfrom which to construct the event.
cdata
)

Constructs a STAX Charactersevent from a DOM4J CDATA.

public Comment

Returns:

The constructed javax.xml.stream.events.Commentevent.
createComment
(Comment
The Commentfrom which to construct the event.
comment
)

Constructs a STAX javax.xml.stream.events.Commentevent from a DOM4J Comment.

public DTD

Returns:

The constructed DTDevent.
createDTD
(DocumentType
The DocumentTypefrom which to construct the event.
docType
)

Constructs a STAX DTDevent from a DOM4J DocumentType.

public EndDocument

Returns:

The constructed EndDocumentevent.
createEndDocument
(Document
The Documentfrom which to construct the event.
doc
)

Constructs a STAX EndDocumentevent from a DOM4J Document.

public EndElement

Returns:

The newly constructed EndElementevent.
createEndElement
(Element
The Elementfrom which to construct the event.
elem
)

Constructs a STAX EndElementevent from a DOM4J Element.

private EntityReference

Returns:

The constructed EntityReferenceevent.
createEntityReference
(Entity
The Entityfrom which to construct the event.
entity
)

Constructs a STAX EntityReferenceevent from a DOM4J Entity.

public Namespace

Returns:

The constructed javax.xml.stream.events.Namespaceevent.
createNamespace
(Namespace
The Namespacefrom which to construct the event.
ns
)

Constructs a STAX javax.xml.stream.events.Namespaceevent from a DOM4J Namespace.

public ProcessingInstruction
public QName

Returns:

The constructed QName.
createQName
(QName
The org.dom4j.QNamefrom which to construct the STAX QName.
qname
)

Constructs a STAX QNamefrom a DOM4J org.dom4j.QName.

public StartDocument

Returns:

The constructed StartDocumentevent.
createStartDocument
(Document
The Documentfrom which to construct the event.
doc
)

Constructs a STAX StartDocumentevent from a DOM4J Document.

public StartElement

Returns:

The newly constructed StartElementevent.
createStartElement
(Element
The Elementfrom which to construct the event.
elem
)

Constructs a STAX StartElementevent from a DOM4J Element.

public XMLEventConsumer

Returns:

The underlying event consumer to which events are written.
getConsumer
()

Returns a reference to the underlying event consumer to which events are written.

public XMLEventFactory

Returns:

The event factory used to construct STAX events.
getEventFactory
()

Returns a reference to the event factory used to construct STAX events.

public void
setConsumer(XMLEventConsumer
The event consumer to which events should be written.
consumer
)

Sets the underlying event consumer to which events are written.

public void
setEventFactory(XMLEventFactory
The new event factory.
eventFactory
)

Sets the event factory used to construct STAX events.

public void
writeAttribute(Attribute
The Attributeto write to the stream.
attr
)

Writes a DOM4J Attributeto the stream.

public void
writeCDATA(CDATA
The CDATAto write to the stream.
cdata
)

Writes a DOM4J CDATAto the event stream.

public void
writeChildNodes(Branch
The node whose children will be written to the stream.
branch
)

Writes each child node within the provided Branchinstance.

public void
writeComment(Comment
The Commentto write to the stream.
comment
)

Writes a DOM4J Commentto the stream.

public void
writeDocument(Document
The Documentto write to the stream.
doc
)

Writes a DOM4J Documentnode, and all its contents, to the stream.

public void
writeDocumentType(DocumentType
The DocumentTypeto write to the stream.
docType
)

Writes a DOM4J DocumentTypeto the stream.

public void
writeElement(Element
The Elementnode to write to the stream.
elem
)

Writes a DOM4J Elementnode and its children to the stream.

public void
writeEntity(Entity
The Entityto write to the stream.
entity
)

Writes a DOM4J Entityto the stream.

public void
writeNamespace(Namespace
The Namespaceto write to the stream.
ns
)

Writes a DOM4J Namespaceto the stream.

public void
writeNode(Node
The DOM4J Nodeto write to the stream.
n
)

Writes a DOM4J Nodeto the stream.

public void
writeProcessingInstruction(ProcessingInstruction
The ProcessingInstructionto write to the stream.
pi
)

Writes a DOM4J ProcessingInstructionto the stream.

public void
writeText(Text
The Textto write to the stream.
text
)

Writes a DOM4J Textto the stream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

consumerback to summary
private XMLEventConsumer consumer

The event stream to which events are written.

factoryback to summary
private XMLEventFactory factory

The event factory used to construct events.

outputFactoryback to summary
private XMLOutputFactory outputFactory

Constructor Detail

STAXEventWriterback to summary
public STAXEventWriter()
STAXEventWriterback to summary
public STAXEventWriter(File file) throws XMLStreamException, IOException

Constructs a STAXEventWriter that writes events to the provided file.

Parameters
file:File

The file to which events will be written.

Exceptions
XMLStreamException:
If an error occurs creating an event writer from the file.
IOException:
If an error occurs openin the file for writing.
STAXEventWriterback to summary
public STAXEventWriter(Writer writer) throws XMLStreamException

Constructs a STAXEventWriter that writes events to the provided character stream.

Parameters
writer:Writer

The character stream to which events will be written.

Exceptions
XMLStreamException:
If an error occurs constructing an event writer from the character stream.
STAXEventWriterback to summary
public STAXEventWriter(OutputStream stream) throws XMLStreamException

Constructs a STAXEventWriter that writes events to the provided stream.

Parameters
stream:OutputStream

The output stream to which events will be written.

Exceptions
XMLStreamException:
If an error occurs constructing an event writer from the stream.
STAXEventWriterback to summary
public STAXEventWriter(XMLEventConsumer consumer)

Constructs a STAXEventWriter that writes events to the provided event stream.

Parameters
consumer:XMLEventConsumer

The event stream to which events will be written.

Method Detail

createAttributeback to summary
public Attribute createAttribute(Attribute attr)

Constructs a STAX javax.xml.stream.events.Attributeevent from a DOM4J Attribute.

Parameters
attr:Attribute

The Attributefrom which to construct the event.

Returns:Attribute

The newly constructed javax.xml.stream.events.Attribute event.

createCharactersback to summary
public Characters createCharacters(Text text)

Constructs a STAX Charactersevent from a DOM4J Text.

Parameters
text:Text

The Textfrom which to construct the event.

Returns:Characters

The constructed Charactersevent.

createCharactersback to summary
public Characters createCharacters(CDATA cdata)

Constructs a STAX Charactersevent from a DOM4J CDATA.

Parameters
cdata:CDATA

The CDATAfrom which to construct the event.

Returns:Characters

The newly constructed Charactersevent.

createCommentback to summary
public Comment createComment(Comment comment)

Constructs a STAX javax.xml.stream.events.Commentevent from a DOM4J Comment.

Parameters
comment:Comment

The Commentfrom which to construct the event.

Returns:Comment

The constructed javax.xml.stream.events.Commentevent.

createDTDback to summary
public DTD createDTD(DocumentType docType)

Constructs a STAX DTDevent from a DOM4J DocumentType.

Parameters
docType:DocumentType

The DocumentTypefrom which to construct the event.

Returns:DTD

The constructed DTDevent.

Exceptions
RuntimeException:
DOCUMENT ME!
createEndDocumentback to summary
public EndDocument createEndDocument(Document doc)

Constructs a STAX EndDocumentevent from a DOM4J Document.

Parameters
doc:Document

The Documentfrom which to construct the event.

Returns:EndDocument

The constructed EndDocumentevent.

createEndElementback to summary
public EndElement createEndElement(Element elem)

Constructs a STAX EndElementevent from a DOM4J Element.

Parameters
elem:Element

The Elementfrom which to construct the event.

Returns:EndElement

The newly constructed EndElementevent.

createEntityReferenceback to summary
private EntityReference createEntityReference(Entity entity)

Constructs a STAX EntityReferenceevent from a DOM4J Entity.

Parameters
entity:Entity

The Entityfrom which to construct the event.

Returns:EntityReference

The constructed EntityReferenceevent.

createNamespaceback to summary
public Namespace createNamespace(Namespace ns)

Constructs a STAX javax.xml.stream.events.Namespaceevent from a DOM4J Namespace.

Parameters
ns:Namespace

The Namespacefrom which to construct the event.

Returns:Namespace

The constructed javax.xml.stream.events.Namespaceevent.

createProcessingInstructionback to summary
public ProcessingInstruction createProcessingInstruction(ProcessingInstruction pi)

Constructs a STAX javax.xml.stream.events.ProcessingInstruction event from a DOM4J ProcessingInstruction.

Parameters
pi:ProcessingInstruction

The ProcessingInstructionfrom which to construct the event.

Returns:ProcessingInstruction

The constructed javax.xml.stream.events.ProcessingInstruction event.

createQNameback to summary
public QName createQName(QName qname)

Constructs a STAX QNamefrom a DOM4J org.dom4j.QName.

Parameters
qname:QName

The org.dom4j.QNamefrom which to construct the STAX QName.

Returns:QName

The constructed QName.

createStartDocumentback to summary
public StartDocument createStartDocument(Document doc)

Constructs a STAX StartDocumentevent from a DOM4J Document.

Parameters
doc:Document

The Documentfrom which to construct the event.

Returns:StartDocument

The constructed StartDocumentevent.

createStartElementback to summary
public StartElement createStartElement(Element elem)

Constructs a STAX StartElementevent from a DOM4J Element.

Parameters
elem:Element

The Elementfrom which to construct the event.

Returns:StartElement

The newly constructed StartElementevent.

getConsumerback to summary
public XMLEventConsumer getConsumer()

Returns a reference to the underlying event consumer to which events are written.

Returns:XMLEventConsumer

The underlying event consumer to which events are written.

getEventFactoryback to summary
public XMLEventFactory getEventFactory()

Returns a reference to the event factory used to construct STAX events.

Returns:XMLEventFactory

The event factory used to construct STAX events.

setConsumerback to summary
public void setConsumer(XMLEventConsumer consumer)

Sets the underlying event consumer to which events are written.

Parameters
consumer:XMLEventConsumer

The event consumer to which events should be written.

setEventFactoryback to summary
public void setEventFactory(XMLEventFactory eventFactory)

Sets the event factory used to construct STAX events.

Parameters
eventFactory:XMLEventFactory

The new event factory.

writeAttributeback to summary
public void writeAttribute(Attribute attr) throws XMLStreamException

Writes a DOM4J Attributeto the stream.

Parameters
attr:Attribute

The Attributeto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeCDATAback to summary
public void writeCDATA(CDATA cdata) throws XMLStreamException

Writes a DOM4J CDATAto the event stream.

Parameters
cdata:CDATA

The CDATAto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeChildNodesback to summary
public void writeChildNodes(Branch branch) throws XMLStreamException

Writes each child node within the provided Branchinstance. This method simply iterates through the Branch's nodes and calls writeNode(Node).

Parameters
branch:Branch

The node whose children will be written to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeCommentback to summary
public void writeComment(Comment comment) throws XMLStreamException

Writes a DOM4J Commentto the stream.

Parameters
comment:Comment

The Commentto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeDocumentback to summary
public void writeDocument(Document doc) throws XMLStreamException

Writes a DOM4J Documentnode, and all its contents, to the stream.

Parameters
doc:Document

The Documentto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeDocumentTypeback to summary
public void writeDocumentType(DocumentType docType) throws XMLStreamException

Writes a DOM4J DocumentTypeto the stream.

Parameters
docType:DocumentType

The DocumentTypeto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeElementback to summary
public void writeElement(Element elem) throws XMLStreamException

Writes a DOM4J Elementnode and its children to the stream.

Parameters
elem:Element

The Elementnode to write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeEntityback to summary
public void writeEntity(Entity entity) throws XMLStreamException

Writes a DOM4J Entityto the stream.

Parameters
entity:Entity

The Entityto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeNamespaceback to summary
public void writeNamespace(Namespace ns) throws XMLStreamException

Writes a DOM4J Namespaceto the stream.

Parameters
ns:Namespace

The Namespaceto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeNodeback to summary
public void writeNode(Node n) throws XMLStreamException

Writes a DOM4J Nodeto the stream. This method is simply a gateway to the overloaded methods such as writeElement(Element).

Parameters
n:Node

The DOM4J Nodeto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeProcessingInstructionback to summary
public void writeProcessingInstruction(ProcessingInstruction pi) throws XMLStreamException

Writes a DOM4J ProcessingInstructionto the stream.

Parameters
pi:ProcessingInstruction

The ProcessingInstructionto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
writeTextback to summary
public void writeText(Text text) throws XMLStreamException

Writes a DOM4J Textto the stream.

Parameters
text:Text

The Textto write to the stream.

Exceptions
XMLStreamException:
If an error occurs writing to the stream.
org.dom4j.io back to summary

private Class STAXEventWriter.AttributeIterator

extends Object
implements Iterator<Attribute>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Internal Iteratorimplementation used to pass DOM4J Attributes to the stream.

Field Summary

Modifier and TypeField and Description
private Iterator<Attribute>
iter

The underlying DOm4J attribute iterator.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public Attribute
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iterback to summary
private Iterator<Attribute> iter

The underlying DOm4J attribute iterator.

Constructor Detail

AttributeIteratorback to summary
public AttributeIterator(Iterator<Attribute> iter)

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

nextback to summary
public Attribute next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:Attribute

the next element in the iteration

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

org.dom4j.io back to summary

private Class STAXEventWriter.NamespaceIterator

extends Object
implements Iterator<Namespace>
Class Inheritance
All Implemented Interfaces
java.util.Iterator

Internal Iteratorimplementation used to pass DOM4J Namespaces to the stream.

Field Summary

Modifier and TypeField and Description
private Iterator<Namespace>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.
public Namespace
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.
public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

iterback to summary
private Iterator<Namespace> iter

Constructor Detail

NamespaceIteratorback to summary
public NamespaceIterator(Iterator<Namespace> iter)

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

Returns:boolean

true if the iteration has more elements

nextback to summary
public Namespace next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

Returns:Namespace

the next element in the iteration

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.