xml_builder v2.1.4 XmlBuilder

A module for generating XML

Examples

iex> XmlBuilder.document(:person) |> XmlBuilder.generate
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person/>"

iex> XmlBuilder.document(:person, "Josh") |> XmlBuilder.generate
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>Josh</person>"

iex> XmlBuilder.document(:person) |> XmlBuilder.generate(format: :none)
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><person/>"

iex> XmlBuilder.element(:person, "Josh") |> XmlBuilder.generate
"<person>Josh</person>"

iex> XmlBuilder.element(:person, %{occupation: "Developer"}, "Josh") |> XmlBuilder.generate
"<person occupation=\"Developer\">Josh</person>"

Link to this section Summary

Functions

Creates a DOCTYPE declaration with a system or public identifier.

Generate an XML document.

Create an XML element.

Generate a binary from an XML tree

Link to this section Functions

Link to this function

doctype(name, list)

Creates a DOCTYPE declaration with a system or public identifier.

System Example

Returns a tuple in the format {:doctype, [:system, name, system_identifier}.

import XmlBuilder

document([
  doctype("greeting", system: "hello.dtd"),
  element(:person, "Josh")
]) |> generate

Outputs

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE greeting SYSTEM "hello.dtd">
<person>Josh</person>

Public Example

Returns a tuple in the format {:doctype, [:public, name, public_identifier, system_identifier}.

import XmlBuilder

document([
  doctype("html", public: ["-//W3C//DTD XHTML 1.0 Transitional//EN",
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"]),
  element(:html, "Hello, world!")
]) |> generate

Outputs

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>Hello, world!</html>
Link to this function

document(elements)

Generate an XML document.

Returns a binary.

Examples

iex> XmlBuilder.document(:person) |> XmlBuilder.generate
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person/>"

iex> XmlBuilder.document(:person, %{id: 1}) |> XmlBuilder.generate
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person id=\"1\"/>"

iex> XmlBuilder.document(:person, %{id: 1}, "some data") |> XmlBuilder.generate
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person id=\"1\">some data</person>"
Link to this function

document(name, attrs_or_content)

Link to this function

document(name, attrs, content)

Create an XML element.

Returns a tuple in the format {name, attributes, content | list}.

Examples

iex> XmlBuilder.element(:person)
{:person, nil, nil}

iex> XmlBuilder.element(:person, "data")
{:person, nil, "data"}

iex> XmlBuilder.element(:person, %{id: 1})
{:person, %{id: 1}, nil}

iex> XmlBuilder.element(:person, %{id: 1}, "data")
{:person, %{id: 1}, "data"}

iex> XmlBuilder.element(:person, %{id: 1}, [XmlBuilder.element(:first, "Steve"), XmlBuilder.element(:last, "Jobs")])
{:person, %{id: 1}, [
  {:first, nil, "Steve"},
  {:last, nil, "Jobs"}
]}
Link to this function

element(name, attrs)

Link to this function

element(name, attrs, content)

Link to this function

generate(any, options \\ [])

Generate a binary from an XML tree

Returns a binary.

Examples

iex> XmlBuilder.generate(XmlBuilder.element(:person))
"<person/>"

iex> XmlBuilder.generate({:person, %{id: 1}, "Steve Jobs"})
"<person id=\"1\">Steve Jobs</person>"

iex> XmlBuilder.generate({:name, nil, [{:first, nil, "Steve"}]}, format: :none)
"<name><first>Steve</first></name>"

iex> XmlBuilder.generate({:name, nil, [{:first, nil, "Steve"}]}, whitespace: "")
"<name>\n<first>Steve</first>\n</name>"

iex> XmlBuilder.generate({:name, nil, [{:first, nil, "Steve"}]})
"<name>\n  <first>Steve</first>\n</name>"

iex> XmlBuilder.generate(:xml_decl, encoding: "ISO-8859-1")
~s|<?xml version="1.0" encoding="ISO-8859-1"?>|