SimpleXmlWriter class

A simple and fast XML writing class that makes it easy to efficiently build an XML string.

Constructors

SimpleXmlWriter(boolean, string, string)

Methods

clear()

Clears all data in the XML writer and sets itself up to be used to write a new document.

toString()

Closes the document and returns a string version of the XML document.

writeCDataContent(string)

Writes a content value inside of a CData tag.

writeComment(string)

Writes a comment in the document.

writeElement(string, any, Record<string, string>)

Writes a complete tag element.

writeEndDocument(boolean)

Writes all the remaining closing tag in the tag name stack so as to close the document.

writeEndElement(boolean)

Writes the closing tag of last tag that was opened.

writeStartDocument(boolean, string, boolean)

Starts an XML document.

writeStartElement(string, Record<string, string | number>, boolean, boolean)

Write a tag element.

writeString(string, boolean)

Writes a string value.

writeValue(any)

Writes a value to XML writer.

Constructor Details

SimpleXmlWriter(boolean, string, string)

new SimpleXmlWriter(prettyPrint?: boolean, newLineChars?: string, indentChars?: string)

Parameters

prettyPrint

boolean

A boolean indicating if the generated XML should be formatted and use new lines and indents. Default: true.

newLineChars

string

The characters to use to create a new line in the XML data. Default: "\r\n"

indentChars

string

The characters to use to create an indent in the XML data. Default: "\t"

Method Details

clear()

Clears all data in the XML writer and sets itself up to be used to write a new document.

function clear()

toString()

Closes the document and returns a string version of the XML document.

function toString(): string

Returns

string

writeCDataContent(string)

Writes a content value inside of a CData tag.

function writeCDataContent(content: string): SimpleXmlWriter

Parameters

content

string

The content value to write.

Returns

writeComment(string)

Writes a comment in the document.

function writeComment(comment: string): SimpleXmlWriter

Parameters

comment

string

The comment to write add to the document.

Returns

writeElement(string, any, Record<string, string>)

Writes a complete tag element.

function writeElement(name: string, content: any, attributes?: Record<string, string>): SimpleXmlWriter

Parameters

name

string

The name of the tag element.

content

any

The content of the tag element.

attributes

Record<string, string>

The attributes for the tag.

Returns

writeEndDocument(boolean)

Writes all the remaining closing tag in the tag name stack so as to close the document.

function writeEndDocument(addTrailingNewLineChar?: boolean): SimpleXmlWriter

Parameters

addTrailingNewLineChar

boolean

A boolean indicating if a trailing new line character should be added or not. Default: true

Returns

writeEndElement(boolean)

Writes the closing tag of last tag that was opened.

function writeEndElement(skipNewLine?: boolean): SimpleXmlWriter

Parameters

skipNewLine

boolean

A boolean indicating if the new line character should not be added after the closing tag. Default: false.

Returns

writeStartDocument(boolean, string, boolean)

Starts an XML document.

function writeStartDocument(includeEncodingTag?: boolean, encoding?: string, standalone?: boolean): SimpleXmlWriter

Parameters

includeEncodingTag

boolean

Specifies if the encoding tag should be added to the document. Default: True.

encoding

string

The string encoding value of the document.

standalone

boolean

A boolean indicating if the XML document is standalone or not.

Returns

writeStartElement(string, Record<string, string | number>, boolean, boolean)

Write a tag element.

function writeStartElement(name: string, attributes?: Record<string, string | number>, selfClosing?: boolean, skipIndent?: boolean): SimpleXmlWriter

Parameters

name

string

The name of this tag.

attributes

Record<string, string | number>

The attributes for this tag.

selfClosing

boolean

A boolean indicating if this tag should be self closing or not.

skipIndent

boolean

Returns

writeString(string, boolean)

Writes a string value.

function writeString(value: string, validate?: boolean): SimpleXmlWriter

Parameters

value

string

The string value to write.

validate

boolean

A boolean indicating if the writer should write the string as-is, or validate and escape of special characters.

Returns

writeValue(any)

Writes a value to XML writer.

function writeValue(value: any): SimpleXmlWriter

Parameters

value

any

Any value that is to be written.

Returns