XML Builder
Overview
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}Installation
Add dependency to your project's mix.exs
def deps do
[{:xml_builder, "~> 0.1.1"}]
endExamples
A simple element
Like <person id="12345">Josh</person>, would look like:
{:person, %{id: 12345}, "Josh"} |> XmlBuilder.generateAn 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.generateConvenience Functions
For more readability, you can use XmlBuilder's methods instead of creating tuples manually.
XmlBuilder.doc(:person, "Josh")Outputs
<?xml version="1.0" encoding="UTF-8" ?>
<person>Josh</person>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)
])
end
iex> [person(123, "Steve", "Jobs"),
person(456, "Steve", "Wozniak")] |> generateOutputs
<person id="123">
<first>Steve</first>
<last>Jobs</last>
</person>"
<person id="456">
<first>Steve</first>
<last>Wozniak</last>
</person>"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)
end
iex> person(123, "Josh", "Nussbaum") |> generate
"<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 doc definition:
import XmlBuilder
doc([
doctype("html", public: ["-//W3C//DTD XHTML 1.0 Transitional//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"]),
element(:html, "Hello, world!")
])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>