View Source XmlBuilder (xml_builder_ex v3.1.4159)
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>"
Summary
Functions
Creates a DOCTYPE declaration with a system or public identifier.
Generate an XML document.
Create an XML element.
See XmlBuilder.Element.element/2
.
See XmlBuilder.Element.element/3
.
Generate a binary from an XML tree. Accepts optional parameters
Similar to generate/2
, but returns iodata
instead of a binary
.
Performs a depth-first, post-order traversal of XML tree.
Performs a depth-first, post-order traversal of XML tree.
Performs a depth-first, pre-order traversal of XML tree.
Performs a depth-first, pre-order traversal of XML tree.
Performs a depth-first traversal of XML tree using an accumulator.
Functions
@spec doctype(XmlBuilder.Element.name(), system: binary(), public: [binary()]) :: {:doctype, {:system, XmlBuilder.Element.name(), binary()} | {:public, XmlBuilder.Element.name(), binary(), binary()}}
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>
@spec document(XmlBuilder.Element.ast()) :: XmlBuilder.Element.ast()
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>"
@spec document( XmlBuilder.Element.name(), XmlBuilder.Element.attrs() | XmlBuilder.Element.contents() ) :: XmlBuilder.Element.ast()
@spec document( XmlBuilder.Element.name(), XmlBuilder.Element.attrs(), XmlBuilder.Element.contents() ) :: XmlBuilder.Element.ast()
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"}
]}
See XmlBuilder.Element.element/2
.
See XmlBuilder.Element.element/3
.
@spec generate( XmlBuilder.Element.ast(), keyword() ) :: String.t()
Generate a binary from an XML tree. Accepts optional parameters
format: Format.Module.Name
to specify the formatter to use, theformat
parameter might be shortened to:none
and:indented
for built-in formattersempty: :full | :squeezed
to not shorten empty tags<foo></foo>
to<foo/>
,:full
will preserve the indentation,:squeezed
would produce closing tag immediately after the opening one, without spaces
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"}]})
"<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"?>|
iex> XmlBuilder.generate(
...> [{:person, %{},
...> [{:name, %{id: 123}, "Josh"},
...> {:age, %{}, "21"}]}], format: XmlBuilder.Format.None)
"<person><name id=\"123\">Josh</name><age>21</age></person>"
iex> XmlBuilder.generate(
...> [{:person, %{},
...> [{:name, %{id: 123}, "Josh"},
...> {:age, %{}, "21"}]}], format: :none)
"<person><name id=\"123\">Josh</name><age>21</age></person>"
@spec generate_iodata( XmlBuilder.Element.ast(), keyword() ) :: iodata()
Similar to generate/2
, but returns iodata
instead of a binary
.
Examples
iex> XmlBuilder.generate_iodata(XmlBuilder.element(:person))
["", '<', "person", '/>']
@spec postwalk(XmlBuilder.Element.ast(), (XmlBuilder.Element.as_tuple() -> XmlBuilder.Element.as_tuple())) :: XmlBuilder.Element.as_tuple()
Performs a depth-first, post-order traversal of XML tree.
Returns a new tree of XmlBuilder.Element.t()
where each element
is the result of invoking fun on each corresponding element.
Examples
iex> XmlBuilder.postwalk(XmlBuilder.element(:person), fn
...> {x, nil, nil} -> {x, %{}, []}
...> end)
{:person, %{}, []}
iex> XmlBuilder.postwalk({:person, %{id: 1}, "Steve Jobs"}, fn
...> {name, args, _content} -> {name, args, "Linux Torvalds"}
...> end)
{:person, %{id: 1}, "Linux Torvalds"}
iex> XmlBuilder.postwalk({:name, nil, [{:first, nil, "Steve"}]}, fn
...> {:first, _, _} -> {:first, %{}, "Linus"}
...> e -> e
...> end)
{:name, nil, [{:first, %{}, "Linus"}]}
iex> XmlBuilder.postwalk(
...> [{:person, %{},
...> [{:name, %{id: 123}, "Josh"},
...> {:age, %{}, "21"}]}], fn
...> {:age, _, _} -> {:age, %{}, "24"}
...> e -> e
...> end)
...> |> hd()
...> |> XmlBuilder.generate(format: XmlBuilder.Format.None)
~s|<person><name id="123">Josh</name><age>24</age></person>|
@spec postwalk(XmlBuilder.Element.ast(), any(), (XmlBuilder.Element.as_tuple(), any() -> {XmlBuilder.Element.as_tuple(), any()})) :: {XmlBuilder.Element.as_tuple(), any()}
Performs a depth-first, post-order traversal of XML tree.
Returns a new tree of XmlBuilder.Element.t()
where each element
is the result of invoking fun on each corresponding element.
Examples
iex> XmlBuilder.postwalk(XmlBuilder.element(:person), %{}, fn
...> {x, nil, nil}, a -> {{x, %{}, []}, a}
...> end)
{{:person, %{}, []}, %{}}
iex> XmlBuilder.postwalk({:person, %{id: 1}, "Steve Jobs"}, %{}, fn
...> {name, args, _content}, a -> {{name, args, "Linux Torvalds"}, a}
...> end)
{{:person, %{id: 1}, "Linux Torvalds"}, %{}}
iex> XmlBuilder.postwalk({:name, nil, [{:first, nil, "Steve"}]}, %{}, fn
...> {:first, _, value}, a ->
...> {{:first, %{}, "Linus"}, Map.put(a, :first, value)}
...> e, a ->
...> {e, a}
...> end)
{{:name, nil, [{:first, %{}, "Linus"}]}, %{first: "Steve"}}
iex> XmlBuilder.postwalk(
...> [{:person, %{},
...> [{:name, %{id: 123}, "Josh"},
...> {:age, %{}, "21"}]}], %{}, fn
...> {:age, _, _}, a -> {{:age, %{}, "24"}, a}
...> e, a -> {e, a}
...> end)
...> |> elem(0)
...> |> hd()
...> |> XmlBuilder.generate(format: XmlBuilder.Format.None)
~s|<person><name id="123">Josh</name><age>24</age></person>|
@spec prewalk(XmlBuilder.Element.ast(), (XmlBuilder.Element.as_tuple() -> XmlBuilder.Element.as_tuple())) :: XmlBuilder.Element.as_tuple()
Performs a depth-first, pre-order traversal of XML tree.
Returns a new tree of XmlBuilder.Element.t()
where each element
is the result of invoking fun on each corresponding element.
Examples
iex> XmlBuilder.prewalk(XmlBuilder.element(:person), fn
...> {x, nil, nil} -> {x, %{}, []}
...> end)
{:person, %{}, []}
iex> XmlBuilder.prewalk({:person, %{id: 1}, "Steve Jobs"}, fn
...> {name, args, _content} -> {name, args, "Linux Torvalds"}
...> end)
{:person, %{id: 1}, "Linux Torvalds"}
iex> XmlBuilder.prewalk({:name, nil, [{:first, nil, "Steve"}]}, fn
...> {:first, _, _} -> {:first, %{}, "Linus"}
...> e -> e
...> end)
{:name, nil, [{:first, %{}, "Linus"}]}
iex> XmlBuilder.prewalk(
...> [{:person, %{},
...> [{:name, %{id: 123}, "Josh"},
...> {:age, %{}, "21"}]}], fn
...> {:age, _, _} -> {:age, %{}, "24"}
...> e -> e
...> end)
...> |> hd()
...> |> XmlBuilder.generate(format: XmlBuilder.Format.None)
~s|<person><name id="123">Josh</name><age>24</age></person>|
@spec prewalk(XmlBuilder.Element.ast(), any(), (XmlBuilder.Element.as_tuple(), any() -> {XmlBuilder.Element.as_tuple(), any()})) :: {XmlBuilder.Element.as_tuple(), any()}
Performs a depth-first, pre-order traversal of XML tree.
Returns a new tree of XmlBuilder.Element.t()
where each element
is the result of invoking fun on each corresponding element.
Examples
iex> XmlBuilder.prewalk(XmlBuilder.element(:person), %{}, fn
...> {x, nil, nil}, a -> {{x, %{}, []}, a}
...> end)
{{:person, %{}, []}, %{}}
iex> XmlBuilder.prewalk({:person, %{id: 1}, "Steve Jobs"}, %{}, fn
...> {name, args, _content}, a -> {{name, args, "Linux Torvalds"}, a}
...> end)
{{:person, %{id: 1}, "Linux Torvalds"}, %{}}
iex> XmlBuilder.prewalk({:name, nil, [{:first, nil, "Steve"}]}, %{}, fn
...> {:first, _, value}, a ->
...> {{:first, %{}, "Linus"}, Map.put(a, :first, value)}
...> e, a ->
...> {e, a}
...> end)
{{:name, nil, [{:first, %{}, "Linus"}]}, %{first: "Steve"}}
iex> XmlBuilder.prewalk(
...> [{:person, %{},
...> [{:name, %{id: 123}, "Josh"},
...> {:age, %{}, "21"}]}], %{}, fn
...> {:age, _, _}, a -> {{:age, %{}, "24"}, a}
...> e, a -> {e, a}
...> end)
...> |> elem(0)
...> |> hd()
...> |> XmlBuilder.generate(format: XmlBuilder.Format.None)
~s|<person><name id="123">Josh</name><age>24</age></person>|
@spec traverse( XmlBuilder.Element.as_tuple(), any(), (XmlBuilder.Element.as_tuple(), any() -> {XmlBuilder.Element.as_tuple(), any()}), (XmlBuilder.Element.as_tuple(), any() -> {XmlBuilder.Element.as_tuple(), any()}) ) :: {XmlBuilder.Element.as_tuple(), any()}
Performs a depth-first traversal of XML tree using an accumulator.