public class XMLWriter extends Object
To write the following XML:
<?xml version='1.0' encoding='UTF-8'?> <xml-doc> <foo a="1" b="2&3"/> <bar>Hello World!</bar> </xml-doc>
One can use the following code:
XMLWriter xmlWriter = new XMLWriter(myWriter); xmlWriter.setPrettyPrint(true); xmlWriter.startDocument(); xmlWriter.startTag("xml-doc"); xmlWriter.setAttribute("a", 1); xmlWriter.setAttribute("b", "2&3"); xmlWriter.simpleTag("foo"); xmlWriter.textTag("bar", "Hello World!"); xmlWriter.endTag("xml-doc"); xmlWriter.endDocument();
Modifier and Type | Field and Description |
---|---|
protected int |
_indentLevel
The current indentation level, i.e.
|
Constructor and Description |
---|
XMLWriter(OutputStream outputStream)
Creates a new XMLWriter that will write its data to the supplied OutputStream in the default UTF-8 character
encoding.
|
XMLWriter(OutputStream outputStream,
String charEncoding)
Creates a new XMLWriter that will write its data to the supplied OutputStream in specified character encoding.
|
XMLWriter(Writer writer)
Creates a new XMLWriter that will write its data to the supplied Writer.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_write(String s)
Writes a string.
|
protected void |
_writeIndent()
Writes as much indentation strings as appropriate for the current indentation level.
|
protected void |
_writeLn(String s)
Writes a string followed by a line-separator.
|
void |
comment(String comment)
Writes a comment.
|
void |
emptyElement(String elName)
Writes an 'empty' element, e.g.
|
void |
emptyLine()
Writes an empty line.
|
void |
endDocument()
Finishes writing and flushes the OutputStream or Writer that this XMLWriter is writing to.
|
void |
endTag(String elName)
Writes an end tag.
|
String |
getIndentString()
Gets the string used for indentation.
|
Writer |
getWriter() |
boolean |
prettyPrintEnabled()
Checks whether pretty-printing is enabled.
|
void |
setAttribute(String name,
boolean value)
Sets an attribute for the next start element.
|
void |
setAttribute(String name,
int value)
Sets an attribute for the next start element.
|
void |
setAttribute(String name,
String value)
Sets an attribute for the next start tag.
|
void |
setIndentString(String indentString)
Sets the string that should be used for indentation when pretty-printing is enabled.
|
void |
setPrettyPrint(boolean prettyPrint)
Enables or disables pretty-printing.
|
void |
startDocument()
Writes the XML header for the XML file.
|
void |
startTag(String elName)
Writes a start tag containing the previously set attributes.
|
void |
text(String text)
Writes a piece of text.
|
void |
textElement(String elName,
boolean value)
Writes a start and end tag with the supplied boolean value between them.
|
void |
textElement(String elName,
int value)
Writes a start and end tag with the supplied value between them.
|
void |
textElement(String elName,
String text)
Writes a start and end tag with the supplied text between them.
|
void |
unescapedTextElement(String elName,
String text)
Writes a start and end tag with the supplied text between them, without the usual escape rules.
|
void |
writeStylesheet(String url)
Writes a link to an XSL stylesheet, using <?xml-stylesheet type='text/xsl' href='url'?>.
|
protected int _indentLevel
public XMLWriter(Writer writer)
writer
- The Writer to write the XML to.public XMLWriter(OutputStream outputStream)
outputStream
- The OutputStream to write the XML to.public XMLWriter(OutputStream outputStream, String charEncoding) throws UnsupportedEncodingException
outputStream
- The OutputStream to write the XML to.UnsupportedEncodingException
public void setPrettyPrint(boolean prettyPrint)
prettyPrint
- Flag indicating whether pretty-printing should be enabled.public boolean prettyPrintEnabled()
public Writer getWriter()
public void setIndentString(String indentString)
indentString
- The indentation string, e.g. a tab or a number of spaces.public String getIndentString()
public void startDocument() throws IOException
IOException
- If an I/O error occurs.public void endDocument() throws IOException
IOException
public void setAttribute(String name, String value)
name
- The name of the attribute.value
- The value of the attribute.public void setAttribute(String name, int value)
name
- The name of the attribute.value
- The value of the attribute. The integer value will be transformed to a string using the method
String.valueOf(int).String.valueOf(int)
public void setAttribute(String name, boolean value)
name
- The name of the attribute.value
- The value of the attribute. The boolean value will be transformed to a string using the method
String.valueOf(boolean).String.valueOf(boolean)
public void startTag(String elName) throws IOException
elName
- The element name.IOException
setAttribute(java.lang.String, java.lang.String)
public void endTag(String elName) throws IOException
elName
- The element name.IOException
public void emptyElement(String elName) throws IOException
elName
- The element name.IOException
setAttribute(java.lang.String, java.lang.String)
public void writeStylesheet(String url) throws IOException
url
- The URL of the stylesheet.IOException
public void textElement(String elName, String text) throws IOException
elName
- The element name.text
- The text.IOException
setAttribute(java.lang.String, java.lang.String)
public void unescapedTextElement(String elName, String text) throws IOException
elName
- The element name.text
- The text.IOException
setAttribute(java.lang.String, java.lang.String)
public void textElement(String elName, int value) throws IOException
elName
- The element name.value
- The value. The integer value will be transformed to a string using the method
String.valueOf(int).IOException
String.valueOf(int)
public void textElement(String elName, boolean value) throws IOException
elName
- The element name.value
- The boolean value. The integer value will be transformed to a string using the method
String.valueOf(boolean).IOException
String.valueOf(boolean)
public void text(String text) throws IOException
text
- The text.IOException
public void comment(String comment) throws IOException
comment
- The comment.IOException
public void emptyLine() throws IOException
IOException
setPrettyPrint(boolean)
protected void _write(String s) throws IOException
IOException
protected void _writeLn(String s) throws IOException
IOException
protected void _writeIndent() throws IOException
IOException
Copyright © 2015-2020 Eclipse Foundation. All Rights Reserved.