Class XmlWriter
java.lang.Object
uk.ac.starlink.util.XmlWriter
Provides methods for writing XML output to a stream.
- Since:
- 17 Mar 2006
- Author:
- Mark Taylor
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new writer which outputs toSystem.out.XmlWriter(PrintStream out) Constructs a new writer which writes to a given print stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddElement(String elName, String attList, String content) Writes a whole element with given attribute list and content.voidendElement(String elName) Outputs an end element tag.static StringformatAttribute(String name, String value) Turns a name,value pair into an attribute assignment suitable for putting in an XML start tag.static StringformatText(String text) Performs necessary special character escaping for text which will be written as XML CDATA.getIndent(int level) Returns the indentation string associated with a given level.intgetLevel()Returns the current element nesting level.voidOutputs a literal string in the output.voidOutputs a literal string in the output followed by a newline character.voidsetOut(PrintStream out) Sets the destination stream for this writer.voidstartElement(String elName) Outputs a start element tag with no attributes.voidstartElement(String elName, String attList) Outputs a start element tag with a given list of attributes.voidWrites an XML declaration.
-
Constructor Details
-
XmlWriter
public XmlWriter()Constructs a new writer which outputs toSystem.out. -
XmlWriter
Constructs a new writer which writes to a given print stream.- Parameters:
out- destination stream
-
-
Method Details
-
writeDeclaration
public void writeDeclaration()Writes an XML declaration. Only call this before any other output. -
startElement
Outputs a start element tag with no attributes.- Parameters:
elName- name of the element
-
startElement
Outputs a start element tag with a given list of attributes. The supplied attribute list is exactly as it will be inserted into the output, so it must start with a space (if it's not empty) and any relevant escaping must have been done.- Parameters:
elName- name of the elementattList- literal string giving the attribute list
-
endElement
Outputs an end element tag.- Parameters:
elName- name of the element- Throws:
IllegalArgumentException- if that element's not ready to finish
-
addElement
Writes a whole element with given attribute list and content. The supplied attribute list and content strings are exactly as they will be inserted into the output, so it must start with a space (if it's not empty) and any relevant escaping must have been done.- Parameters:
elName- name of the elementattList- literal string giving the attribute listcontent- literal string giving the element content
-
print
Outputs a literal string in the output.- Parameters:
txt- literal text
-
setOut
Sets the destination stream for this writer.- Parameters:
out- new destination stream
-
println
Outputs a literal string in the output followed by a newline character.- Parameters:
txt- literal text
-
getLevel
public int getLevel()Returns the current element nesting level.- Returns:
- nesting level (0 at start and end of document)
-
formatAttribute
Turns a name,value pair into an attribute assignment suitable for putting in an XML start tag. The resulting string starts with, but does not end with, whitespace. Any necessary escaping of the strings is taken care of.- Parameters:
name- the attribute namevalue- the attribute value- Returns:
- string of the form ' name="value"'
-
formatText
-
getIndent
Returns the indentation string associated with a given level. This is a couple of spaces for each level.- Returns:
- level
-