Class: XMLBuilder

XMLBuilder()

Simplistic implementation of a builder for XML strings. The values for all tags, attributes, attribute values and content have reserved characters replaced with their corresponding XML entity, regardless if such replacement is explicitly necessary (with the exception of calls to XMLBuilder#appendDirect in which the given value is appended to the underlying data as-is).

By default, the underlying data is maintained using string concatenation, however if the execution environment handles Array-based concatenation more efficiently, set XMLBuilder.useArray to true.

This implementation is state-based, and therefore, not thread-safe.

Constructor

new XMLBuilder()

Default constructor.

Source:

Members

(private, static, constant) ESCAPE_CHARACTER_MAPPING :object

Mapping between reserved characters and thier replacement XML entities.

Type:
  • object
Source:

(private, static, constant) STATE_CONTENT :number

Between-tag state constant.

Type:
  • number
Source:

(private, static, constant) STATE_INITIAL :number

Initial state constant.

Type:
  • number
Source:

(private, static, constant) STATE_TAG :number

Within-tag (i.e. attribute definitions) state constant.

Type:
  • number
Source:

(private, static) useArray :boolean

Whether or not to use an Array to store the underlying data within XMLBuilder instances.

Type:
  • boolean
Source:

(private) stack :Array

Tag name stack.

Type:
  • Array
Source:

(private) state :number

Current state of this XMLBuilder. Corresponds to one of the STATE_* static members of this class.

Type:
  • number
Source:

(private) text :string|Array

The raw text of this XMLBuilder. If XMLBuilder.useArray is true, it will be an Array, otherwise a string.

Type:
  • string | Array
Source:

Methods

(static) escape(val) → {string}

Replaces all occurences of XML reserved characters in val with their corresponding XML entity.

Parameters:
Name Type Description
val string

Value to escape.

Source:
Returns:

The given val with XML reserved characters replaced with their corresponding entities.

Type
string

appendDirect(value) → {XMLBuilder}

Appends the given value directly to this XMLBuilder without the escaping of reserved characters, nor concern for this builder's current state.

Parameters:
Name Type Description
value string

Value to append.

Source:
Returns:

This XMLBuilder.

Type
XMLBuilder

attribute(name, valueopt) → {XMLBuilder}

Adds an attribute with the given name and, optionally, the given value to the current tag. If Nothing is passed for value, the given attribute is added as a 'boolean' attribute (i.e. no equals sign). If it is desired an attribute value be explicitly set to a value that is otherwise Nothing, convert value to a string prior to passage to this function.

If not called subsequent to a call to XMLBuilder#startTag, an Error will be thrown.

Parameters:
Name Type Attributes Default Description
name string

Attribute name to add.

value string <optional>
''

Attribute value.

Source:
Throws:

If not called subsequent to a call to XMLBuilder#startTag.

Type
Error
Returns:

This XMLBuilder.

Type
XMLBuilder

clear()

Clears this XMLBuilder.

Source:

closeTag(requiresBodyopt) → {XMLBuilder}

Closes the current tag. If the tag has content, or requiresBody is true, the tag will be closed in the 'traditional' manner (i.e. '</tagName>'), otherwise the tag will be closed as a no-body tag (i.e. '/>'). If there is no current tag, an Error will be thrown.

Parameters:
Name Type Attributes Default Description
requiresBody boolean <optional>
false

Whether the tag being closed requires a body.

Source:
Throws:

If there is no current tag.

Type
Error
Returns:

This XMLBuilder.

Type
XMLBuilder

content(value) → {XMLBuilder}

Adds content (i.e. text) to the current tag.

Parameters:
Name Type Description
value string

Content to append to the current tag's content.

Source:
Returns:

This XMLBuilder.

Type
XMLBuilder

startTag(name) → {XMLBuilder}

Starts an XML tag. Subsequent calls to XMLBuilder#attribute will define attributes on this tag. Calling XMLBuilder#content will close this tag opening (i.e. append the '>' character), and begin defining the content of this tag's body. Calling XMLBuilder#startTag again will also close this tag's opening, but start another tag as a child of this tag.

Parameters:
Name Type Description
name string

Name of the tag to be started.

Source:
Returns:

This XMLBuilder.

Type
XMLBuilder

toString() → {string}

Returns this XMLBuilder's current content as a string.

Source:
Returns:

This XMLBuilder's content.

Type
string