# class XML::Builder

- XML::Builder
- Reference
- Object

##
Overview

An XML builder generates valid XML.

An `XML::Error` is raised if attempting to generate an invalid XML (for example, if invoking `#end_element` without a matching `#start_element`, or trying to use a non-string value as an object's field name)

##
Defined in:

xml/builder.cr

##
Constructors

- .new(io : IO)

  Creates a builder that writes to the given io.

##
Instance Method Summary

- #attribute(*args, **nargs, &)

  Emits the start of an attribute, invokes the block, and then emits the end of the attribute.

- #attribute(prefix : String | Nil, name : String, namespace_uri : String | Nil, value) : Nil

  Emits an attribute with namespace info and a value.

- #attribute(name : String, value) : Nil

  Emits an attribute with a value.

- #attributes(attributes : Hash | NamedTuple) : Nil

  Emits the given attributes with their values.

- #attributes(**attributes)

  Emits the given attributes with their values.

- #cdata(&)

  Emits the start of a `CDATA` section, invokes the block and then emits the end of the `CDATA` section.

- #cdata(text : String) : Nil

  Emits a `CDATA` section.

- #comment(&)

  Emits the start of a comment, invokes the block and then emits the end of the comment.

- #comment(text : String) : Nil

  Emits a comment.

- #document(version = nil, encoding = nil, &)

  Emits the start of the document, invokes the block, and then emits the end of the document.

- #dtd(name : String, pubid : String, sysid : String, &) : Nil

  Emits the start of a `DTD`, invokes the block and then emits the end of the `DTD`.

- #dtd(name : String, pubid : String, sysid : String, subset : String | Nil = nil) : Nil

  Emits a `DTD`.

- #element(__prefix__ : String | Nil, __name__ : String, __namespace_uri__ : String | Nil, attributes : Hash | NamedTuple, &)

  Emits the start of an element with namespace info with the given attributes, invokes the block and then emits the end of the element.

- #element(__prefix__ : String | Nil, __name__ : String, __namespace_uri__ : String | Nil, **attributes, &)

  Emits the start of an element with namespace info with the given attributes, invokes the block and then emits the end of the element.

- #element(__name__ : String, attributes : Hash | NamedTuple, &)

  Emits the start of an element with the given attributes, invokes the block and then emits the end of the element.

- #element(__name__ : String, **attributes, &)

  Emits the start of an element with the given attributes, invokes the block and then emits the end of the element.

- #element(prefix : String | Nil, name : String, namespace_uri : String | Nil, attributes : Hash | NamedTuple) : Nil

  Emits an element with namespace info with the given attributes.

- #element(prefix : String | Nil, name : String, namespace_uri : String | Nil, **attributes) : Nil

  Emits an element with namespace info with the given attributes.

- #element(name : String, attributes : Hash | NamedTuple) : Nil

  Emits an element with the given attributes.

- #element(__name__ : String, **attributes)

  Emits an element with the given attributes.

- #end_attribute : Nil

  Emits the end of an attribute.

- #end_cdata : Nil

  Emits the end of a `CDATA` section.

- #end_comment : Nil

  Emits the end of a comment.

- #end_document : Nil

  Emits the end of a document.

- #end_dtd : Nil

  Emits the end of a `DTD`.

- #end_element : Nil

  Emits the end of an element.

- #flush : Nil

  Forces content written to this writer to be flushed to this writer's `IO`.

- #indent=(str : String)

  Sets the indent string.

- #indent=(level : Int)

  Sets the indent level (number of spaces).

- #namespace(prefix, uri) : Nil

  Emits a namespace.

- #quote_char=(char : Char)

  Sets the quote char to use, either `'` or `"`.

- #start_attribute(prefix : String | Nil, name : String, namespace_uri : String | Nil)

  Emits the start of an attribute with namespace info.

- #start_attribute(name : String) : Nil

  Emits the start of an attribute.

- #start_cdata : Nil

  Emits the start of a `CDATA` section.

- #start_comment : Nil

  Emits the start of a comment.

- #start_document(version = nil, encoding = nil) : Nil

  Emits the start of the document.

- #start_dtd(name : String, pubid : String, sysid : String) : Nil

  Emits the start of a `DTD`.

- #start_element(prefix : String | Nil, name : String, namespace_uri : String | Nil) : Nil

  Emits the start of an element with namespace info.

- #start_element(name : String) : Nil

  Emits the start of an element.

- #text(content : String) : Nil

  Emits text content.

### Instance methods inherited from class `Reference`

==(other : self)
==(other : JSON::Any)
==(other : YAML::Any)
==(other) ==, dup dup, hash(hasher) hash, initialize initialize, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference) : Bool
same?(other : Nil) same?, to_s(io : IO) : Nil to_s

### Constructor methods inherited from class `Reference`

new new, unsafe_construct(address : Pointer, *args, **opts) : self unsafe_construct

### Class methods inherited from class `Reference`

pre_initialize(address : Pointer) pre_initialize

### Instance methods inherited from class `Object`

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other) ===, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash hash, in?(collection : Object) : Bool
in?(*values : Object) : Bool in?, inspect(io : IO) : Nil
inspect : String inspect, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil!(message)
not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO) : Nil
to_json : String to_json, to_pretty_json(indent : String = " ") : String
to_pretty_json(io : IO, indent : String = " ") : Nil to_pretty_json, to_s(io : IO) : Nil
to_s : String to_s, to_yaml(io : IO) : Nil
to_yaml : String to_yaml, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

### Class methods inherited from class `Object`

from_json(string_or_io, root : String)
from_json(string_or_io) from_json, from_yaml(string_or_io : String | IO) from_yaml

### Macros inherited from class `Object`

class_getter(*names, &block) class_getter, class_getter!(*names) class_getter!, class_getter?(*names, &block) class_getter?, class_property(*names, &block) class_property, class_property!(*names) class_property!, class_property?(*names, &block) class_property?, class_setter(*names) class_setter, def_clone def_clone, def_equals(*fields) def_equals, def_equals_and_hash(*fields) def_equals_and_hash, def_hash(*fields) def_hash, delegate(*methods, to object) delegate, forward_missing_to(delegate) forward_missing_to, getter(*names, &block) getter, getter!(*names) getter!, getter?(*names, &block) getter?, property(*names, &block) property, property!(*names) property!, property?(*names, &block) property?, setter(*names) setter

##
Constructor Detail

def self.new(io : IO) #

Creates a builder that writes to the given io.

##
Instance Method Detail

def attribute(*args, **nargs, &) #

Emits the start of an attribute, invokes the block, and then emits the end of the attribute.

def attribute(prefix : String | Nil, name : String, namespace_uri : String | Nil, value) : Nil #

Emits an attribute with namespace info and a value.

def attribute(name : String, value) : Nil #

Emits an attribute with a value.

def attributes(attributes : Hash | NamedTuple) : Nil #

Emits the given attributes with their values.

def attributes(**attributes) #

Emits the given attributes with their values.

def cdata(&) #

Emits the start of a `CDATA` section, invokes the block and then emits the end of the `CDATA` section.

NOTE `CDATA` end sequences written within the block need to be escaped manually.

def cdata(text : String) : Nil #

Emits a `CDATA` section. Escapes nested `CDATA` end sequences.

def comment(&) #

Emits the start of a comment, invokes the block and then emits the end of the comment.

def comment(text : String) : Nil #

Emits a comment.

def document(version = nil, encoding = nil, &) #

Emits the start of the document, invokes the block, and then emits the end of the document.

def dtd(name : String, pubid : String, sysid : String, &) : Nil #

Emits the start of a `DTD`, invokes the block and then emits the end of the `DTD`.

def dtd(name : String, pubid : String, sysid : String, subset : String | Nil = nil) : Nil #

Emits a `DTD`.

def element(__prefix__ : String | Nil, __name__ : String, __namespace_uri__ : String | Nil, attributes : Hash | NamedTuple, &) #

Emits the start of an element with namespace info with the given attributes, invokes the block and then emits the end of the element.

def element(__prefix__ : String | Nil, __name__ : String, __namespace_uri__ : String | Nil, **attributes, &) #

Emits the start of an element with namespace info with the given attributes, invokes the block and then emits the end of the element.

def element(__name__ : String, attributes : Hash | NamedTuple, &) #

Emits the start of an element with the given attributes, invokes the block and then emits the end of the element.

def element(__name__ : String, **attributes, &) #

Emits the start of an element with the given attributes, invokes the block and then emits the end of the element.

def element(prefix : String | Nil, name : String, namespace_uri : String | Nil, attributes : Hash | NamedTuple) : Nil #

Emits an element with namespace info with the given attributes.

def element(prefix : String | Nil, name : String, namespace_uri : String | Nil, **attributes) : Nil #

Emits an element with namespace info with the given attributes.

def element(name : String, attributes : Hash | NamedTuple) : Nil #

Emits an element with the given attributes.

def element(__name__ : String, **attributes) #

Emits an element with the given attributes.

def end_attribute : Nil #

Emits the end of an attribute.

def end_cdata : Nil #

Emits the end of a `CDATA` section.

def end_comment : Nil #

Emits the end of a comment.

def end_document : Nil #

Emits the end of a document.

def end_dtd : Nil #

Emits the end of a `DTD`.

def end_element : Nil #

Emits the end of an element.

def flush : Nil #

Forces content written to this writer to be flushed to this writer's `IO`.

def indent=(str : String) #

Sets the indent string.

def indent=(level : Int) #

Sets the indent level (number of spaces).

def namespace(prefix, uri) : Nil #

Emits a namespace.

def quote_char=(char : Char) #

Sets the quote char to use, either `'` or `"`.

def start_attribute(prefix : String | Nil, name : String, namespace_uri : String | Nil) #

Emits the start of an attribute with namespace info.

def start_attribute(name : String) : Nil #

Emits the start of an attribute.

def start_cdata : Nil #

Emits the start of a `CDATA` section.

def start_comment : Nil #

Emits the start of a comment.

def start_document(version = nil, encoding = nil) : Nil #

Emits the start of the document.

def start_dtd(name : String, pubid : String, sysid : String) : Nil #

Emits the start of a `DTD`.

def start_element(prefix : String | Nil, name : String, namespace_uri : String | Nil) : Nil #

Emits the start of an element with namespace info.

def start_element(name : String) : Nil #

Emits the start of an element.

def text(content : String) : Nil #

Emits text content.

Text content can happen inside of an `#element`, `#attribute` value, `#cdata`, `#dtd`, etc.