Skip to content
Elixir library for generating XML
Branch: master
Clone or download
Latest commit 0d47e93 Nov 8, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
config Initial commit Jul 16, 2014
lib [SJC-152] Add missing special character encoding Sep 19, 2019
test [SJC-152] Add missing special character encoding Sep 19, 2019
.gitignore Initial commit Jul 16, 2014
.travis.yml Adds config for TravisCI Jan 5, 2017 Update Jun 13, 2019
mix.exs Bump version to 2.1.2 Nov 9, 2019
mix.lock [SJC-152] Add missing special character encoding Sep 19, 2019

XML Builder

Build Status


An Elixir library for building xml.

Each xml node is structured as a tuple of name, attributes map and content/list:

{name, attrs, content | list}


Add dependency to your project's mix.exs

def deps do
  [{:xml_builder, "~> 2.1.1"}]


A simple element

Like <person id="12345">Josh</person>, would look like:

{:person, %{id: 12345}, "Josh"} |> XmlBuilder.generate

An element with child elements

Like <person id="12345"><first>Josh</first><last>Nussbaum</last></person>

{:person, %{id: 12345}, [{:first, nil, "Josh"}, {:last, nil, "Nussbaum"}]} |> XmlBuilder.generate

Convenience Functions

For more readability, you can use XmlBuilder's methods instead of creating tuples manually.

XmlBuilder.document(:person, "Josh") |> XmlBuilder.generate


<?xml version="1.0" encoding="UTF-8" ?>

Building up an element

An element can be built using multiple calls to the element function

import XmlBuilder

def person(id, first, last) do
  element(:person, %{id: id}, [
    element(:first, first),
    element(:last, last)

iex> [person(123, "Steve", "Jobs"),
      person(456, "Steve", "Wozniak")] |> generate


<person id="123">
<person id="456">

Using keyed lists

The previous example can be simplified using a keyed list

import XmlBuilder

def person(id, first, last) do
  element(:person, %{id: id}, first: first,
                              last: last)

iex> person(123, "Josh", "Nussbaum") |> generate(format: :none)
"<person id=\"123\"><first>Josh</first><last>Nussbaum</last></person>"

DOCTYPE declarations

A DOCTYPE can be declared by applying the doctype function at the first position of a list of elements in a document definition:

import XmlBuilder

  doctype("html", public: ["-//W3C//DTD XHTML 1.0 Transitional//EN",
  element(:html, "Hello, world!")
]) |> generate


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html>Hello, world!</html>


While the output is always UTF-8 and has to be converted in another place, you can override the encoding statement in the xml declaration with the encoding option:

import XmlBuilder

[XmlBuilder.element(:oldschool, [])]
|> XmlBuilder.document()
|> XmlBuilder.generate(encoding: "ISO-8859-1")
|> :unicode.characters_to_binary(:unicode, :latin1)


<?xml version="1.0" encoding="ISO-8859-1"?>


To remove indentation, pass format: :none option to XmlBuilder.generate/2:

doc |> XmlBuilder.generate(format: :none)

The default is to formatting with indentation, which is equivalent to XmlBuilder.generate(doc, format: :indent)



You can’t perform that action at this time.