Package eu.woolplatform.utils.xml
Class XMLWriter
- java.lang.Object
-
- eu.woolplatform.utils.xml.XMLWriter
-
public class XMLWriter extends Object
An XML writer provides convenient methods to produce XML code.
-
-
Constructor Summary
Constructors Constructor Description XMLWriter(OutputStream out)Constructs a new XML writer that will write XML code to the specified output stream in UTF-8 encoding.XMLWriter(Writer writer)Constructs a new XML writer that will write XML code to the specified writer.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this XML writer.static StringescapeString(String s)Escapes any special XML characters with XML entities.voidwriteAttribute(String name, String value)Writes an attribute of an element.voidwriteCharacters(String s)Writes characters.voidwriteElement(String xml)Writes a complete XML element.voidwriteElement(Element elem)Writes a complete XML element.voidwriteEndElement()Writes the end of an element.voidwriteStartElement(String name)Writes the start of an element.
-
-
-
Constructor Detail
-
XMLWriter
public XMLWriter(OutputStream out) throws IOException
Constructs a new XML writer that will write XML code to the specified output stream in UTF-8 encoding. This constructor will immediately write the XML prolog.- Parameters:
out- the output stream- Throws:
IOException- if an error occurs while initialising the XML writer
-
XMLWriter
public XMLWriter(Writer writer) throws IOException
Constructs a new XML writer that will write XML code to the specified writer. If the code is encoded some time, it should be encoded in UTF-8. This constructor will immediately write the XML prolog.- Parameters:
writer- the writer- Throws:
IOException- if an error occurs while initialising the XML writer
-
-
Method Detail
-
close
public void close()
Closes this XML writer.
-
writeStartElement
public void writeStartElement(String name) throws IOException
Writes the start of an element.- Parameters:
name- the element name- Throws:
IOException- if a writing error occurs
-
writeAttribute
public void writeAttribute(String name, String value) throws IOException
Writes an attribute of an element. This can only be called afterwriteStartElement(). Any special characters in the attribute value will be escaped with XML entities.- Parameters:
name- the attribute namevalue- the attribute value- Throws:
IOException- if the writer is not in the start of an element, or if a writing error occurs
-
writeEndElement
public void writeEndElement() throws IOExceptionWrites the end of an element. This can only be called ifwriteStartElement()was called earlier. Eventually it should be called once for each call towriteStartElement().- Throws:
IOException- if no matching start was found, or if a writing error occurs
-
writeCharacters
public void writeCharacters(String s) throws IOException
Writes characters. Any special characters in the string will be escaped with XML entities.- Parameters:
s- the characters- Throws:
IOException- if a writing error occurs
-
writeElement
public void writeElement(String xml) throws IOException
Writes a complete XML element. It parses the XML code into an element and then callswriteElement(Element).- Parameters:
xml- the XML code for the element- Throws:
IOException- if the XML code is not a valid element or a writing error occurs
-
writeElement
public void writeElement(Element elem) throws IOException
Writes a complete XML element.- Parameters:
elem- the element- Throws:
IOException- if a writing error occurs
-
-