Class DOMWriterImpl
Provides methods to generate well-formed XML data easily via DOM.
- Version:
- 11 December 2011
-
Field Summary
FieldsModifier and TypeFieldDescriptionAn array to indicate which elements have children.private NodeThe current node being written onto.private intLevel of the depth of the xml document currently produced.private final DocumentThe DOM document on which we write.private booleanIndicates whether the xml should be indented or not.private StringThe default indentation spaces used.private booleanFlag to indicate that the element open tag is not finished yet.private final NodeThe new line used. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new XML writer for DOM using the default implementation on the system.DOMWriterImpl(Document document) Creates a new XML writer for DOM. -
Method Summary
Modifier and TypeMethodDescriptionvoidWrites an attribute.voidWrites an attribute.voidNot supported.voidNot supported.voidclose()Does nothing.voidClose the element automatically.private voiddeNude()Writes the angle bracket if the element open tag is not finished.voidOpens element, inserts text node and closes.voidemptyElement(String name) Writes an empty element.voidemptyElement(String uri, String element) Not supported.voidflush()Normalises the current element.Returns the DOM document produced by the XML Writer.(package private) voidindent()Insert the correct amount of space characterss depending on the depth and if theindentflag is set totrue.private static DocumentReturns a new DOM document.private voidnewLine()Adds a new line to the DOM.voidopenElement(String name) Writes a start element tag correctly indented.voidopenElement(String name, boolean hasChildren) Writes a start element tag correctly indented.voidopenElement(String uri, String name) Not supported.voidopenElement(String uri, String name, boolean hasChildren) Not supported.voidsetIndentChars(String spaces) Sets the string to use for indentation.voidsetPrefixMapping(String uri, String prefix) Not supported.voidwriteCDATA(String data) Writes the CDATA section to the DOM.voidwriteComment(String comment) Writes an XML comment.voidWrites an XML processing instruction.voidwriteText(char c) This method is expensive as the character has to be converted to a String for DOM.voidwriteText(char[] text, int off, int len) Write the given text correctly for the encoding of this document.voidWrites the string value of an object.voidWrites the given text correctly for the encoding of this document.voidwriteXML(char[] text, int off, int len) Write the given XML data.voidWrites the given XML data.voidxmlDecl()Does nothing.
-
Field Details
-
document
The DOM document on which we write. -
newline
The new line used. -
indent
private boolean indentIndicates whether the xml should be indented or not.The default is
true(indented).The indentation is 2 white-spaces.
-
indentChars
The default indentation spaces used. -
depth
private transient int depthLevel of the depth of the xml document currently produced.This attribute changes depending on the state of the instance.
-
isNude
private transient boolean isNudeFlag to indicate that the element open tag is not finished yet. -
currentElement
The current node being written onto.This node should always be an element except before and after writing where it is the document node itself.
-
childrenFlags
An array to indicate which elements have children.
-
-
Constructor Details
-
DOMWriterImpl
Creates a new XML writer for DOM using the default implementation on the system.
Attempts to create the DOM document using:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument();
- Throws:
ParserConfigurationException- If thrown by the document builder factory.
-
DOMWriterImpl
Creates a new XML writer for DOM.
- Parameters:
document- The DOM provided.- Throws:
NullPointerException- If the handler isnull.
-
-
Method Details
-
xmlDecl
public void xmlDecl()Does nothing. -
setIndentChars
Sets the string to use for indentation.The string must be only composed of valid spaces characters.
If the string is
nullthen the indentation is turned off.- Specified by:
setIndentCharsin interfaceXMLWriter- Parameters:
spaces- The indentation string to use.- See Also:
-
writeText
Writes the given text correctly for the encoding of this document.Does nothing if the text is
null.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
text- The text to write- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
writeText
public void writeText(char[] text, int off, int len) Write the given text correctly for the encoding of this document.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
text- The text to write.off- The offset where we should start writing the string.len- The length of the character subarray to write.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
writeText
public void writeText(char c) This method is expensive as the character has to be converted to a String for DOM. Writes the given character correctly for the encoding of this document.- Specified by:
writeTextin interfaceXMLWriter- Parameters:
c- The character to write.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
writeText
Writes the string value of an object.Does nothing if the object is
null.- Parameters:
o- The object that should be written as text.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document- See Also:
-
writeCDATA
Writes the CDATA section to the DOM.Does nothing if the object is
null.- Specified by:
writeCDATAin interfaceXMLWriter- Parameters:
data- The data to write to the section.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
writeXML
Writes the given XML data.The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
Does nothing if the text is
null.- Specified by:
writeXMLin interfaceXMLWriter- Parameters:
text- The text to write.- Throws:
UnsupportedOperationException- XML cannot be written to the DOM
-
writeXML
Write the given XML data.The text is appended as is, therefore it should be escaped properly for the encoding used by the underlying stream writer.
- Specified by:
writeXMLin interfaceXMLWriter- Parameters:
text- The text to write.off- The offset where we should start writing the string.len- The length of the character subarray to write.- Throws:
UnsupportedOperationException- XML cannot be written to the DOM
-
writeComment
Writes an XML comment.An XML comment is:
<!-- comment -->
Comments are not indented.
Does not write anything if the comment if
null.- Specified by:
writeCommentin interfaceXMLWriter- Parameters:
comment- The comment to be written- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
writePI
Writes an XML processing instruction.An XML processing intruction is:
<?target data?>
- Specified by:
writePIin interfaceXMLWriter- Parameters:
target- The PI's target.data- The PI's data.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
attribute
Writes an attribute.- Specified by:
attributein interfaceXMLWriter- Parameters:
name- The name of the attribute.value- The value of the attribute.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
attribute
Writes an attribute.This method for number does not require escaping.
- Specified by:
attributein interfaceXMLWriter- Parameters:
name- The name of the attribute.value- The value of the attribute.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
openElement
Writes a start element tag correctly indented.It is the same as
openElement("", name, false)- Specified by:
openElementin interfaceXMLWriter- Parameters:
name- the name of the element- Throws:
DOMException- If thrown by method invoked on the underlying DOM document- See Also:
-
openElement
Writes a start element tag correctly indented.Use the
hasChildrenparameter to specify whether this element is terminal node or not, note: this affects the indenting. To produce correctly indented XML, you should use the same value for this flag when closing the element.The name can contain attributes and should be a valid xml name.
- Specified by:
openElementin interfaceXMLWriter- Parameters:
name- The name of the element.hasChildren-trueif this element has children.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
element
Opens element, inserts text node and closes.This method should behave like:
this.openElement(name, false); this.writeText(text); this.closeElement();
- Specified by:
elementin interfaceXMLWriter- Parameters:
name- The name of the element.text- The text of the element.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
closeElement
Close the element automatically.The element is closed symmetrically to the
XMLWriter.openElement(String, String, boolean)method if the XML writer is namespace aware or theXMLWriter.openElement(String, boolean)method.- Specified by:
closeElementin interfaceXMLWriter- Throws:
DOMException- If thrown by method invoked on the underlying DOM documentIllegalCloseElementException
-
emptyElement
Writes an empty element.It is possible for the element to contain attributes, however, since there is no character escaping, great care must be taken not to introduce invalid characters. For example:
<example test="yes"/>- Specified by:
emptyElementin interfaceXMLWriter- Parameters:
name- the name of the element- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
close
public void close()Does nothing. Close the writer. -
flush
public void flush()Normalises the current element. -
getDocument
Returns the DOM document produced by the XML Writer.- Specified by:
getDocumentin interfaceDOMWriter- Returns:
- The DOM document produced by the XML Writer.
-
openElement
Not supported.- Parameters:
uri- This parameter is ignored.name- This parameter is ignored.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
openElement
public void openElement(String uri, String name, boolean hasChildren) throws UnsupportedOperationException Not supported.- Specified by:
openElementin interfaceXMLWriter- Parameters:
uri- This parameter is ignored.name- This parameter is ignored.hasChildren- This parameter is ignored.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
emptyElement
Not supported.- Specified by:
emptyElementin interfaceXMLWriter- Parameters:
uri- This parameter is ignored.element- This parameter is ignored.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
setPrefixMapping
Not supported.- Specified by:
setPrefixMappingin interfaceXMLWriter- Parameters:
uri- This parameter is ignored.prefix- This parameter is ignored.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
attribute
Not supported.- Specified by:
attributein interfaceXMLWriter- Parameters:
uri- This parameter is ignored.name- The name of the attribute.value- The value of the attribute.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
attribute
Not supported.- Specified by:
attributein interfaceXMLWriter- Parameters:
uri- This parameter is ignored.name- The name of the attribute.value- The value of the attribute.- Throws:
UnsupportedOperationException- This class does not handle namespaces.
-
indent
void indent()Insert the correct amount of space characterss depending on the depth and if theindentflag is set totrue. -
deNude
private void deNude()Writes the angle bracket if the element open tag is not finished. -
newLine
private void newLine()Adds a new line to the DOM.- Throws:
DOMException- If thrown by method invoked on the underlying DOM document
-
newDocument
Returns a new DOM document.Attempts to create the DOM document using:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.newDocument();
- Returns:
- A new DOM document.
- Throws:
ParserConfigurationException- If thrown by the document builder factory.
-