builder.xml.Generator
class pub Generator
A type for generating an XML String
from a Document
.
Static methods
new
Show source codeHide source code
fn pub static new(indent: String) -> Generator {
Generator(buffer: StringBuffer.new, indent: indent)
}
fn pub static new(indent: String) -> Generator
Returns a new Generator
that formats the XML with the given indentation
Examples
Generating XML without any indentation:
import builder.xml.(Document, Generator)
let doc = Document.new
doc.element('person').with fn (p) { p.element('name') }
Generator
.new(indent: '')
.generate(doc) # => '<person><name></name></person>'
Using two spaces for indentation:
import builder.xml.(Document, Generator)
let doc = Document.new
doc.element('person').with fn (p) { p.element('name') }
Generator
.new(indent: ' ')
.generate(doc) # => "<person>\n <name></name>\n</person>"
Instance methods
generate
Show source codeHide source code
fn pub move generate(document: ref Document) -> String {
# Inko Strings are UTF-8, so by definition the generated document is also in
# UTF-8.
@buffer.push('<?xml version="1.0" encoding="UTF-8" ?>')
if indent? { @buffer.push('\n') }
document.nodes.iter.each(fn (node) { node(node, depth: 0) })
@buffer.into_string
}
fn pub move generate(document: ref Document) -> String
Returns a String
containing the XML representation of the given document.