XMap v0.2.3 XMap View Source

XML to Map conversion.

XMap transforms an XML string into a Map containing a collection of pairs where the key is the node name and the value is its content.

Examples

Here is an example:

iex> xml = """
...> <?xml version="1.0" encoding="UTF-8"?>
...> <blog>
...>   <post>
...>     <title>Hello Elixir!</title>
...>   </post>
...>   <post>
...>     <title>Hello World!</title>
...>   </post>
...> </blog>
...> """
iex> XMap.from_xml(xml)
%{"blog" => %{"post" => [%{"title" => "Hello Elixir!"},
                         %{"title" => "Hello World!"}]}}
iex> XMap.from_xml(xml, keys: :atoms)
%{blog: %{post: [%{title: "Hello Elixir!"}, %{title: "Hello World!"}]}}

Keys can be converted to atoms with the keys: :atoms option. Unless you absolutely know what you’re doing, do not use the keys: :atoms option. Atoms are not garbage-collected, see Erlang Efficiency Guide for more info:

Atoms are not garbage-collected. Once an atom is created, it will never be removed. The emulator will terminate if the limit for the number of atoms (1048576 by default) is reached.

Link to this section Summary

Functions

Returns a Map containing a collection of pairs where the key is the node name and the value is its content

Link to this section Functions

Link to this function from_xml(xml, options \\ []) View Source
from_xml(String.t, keyword) :: map

Returns a Map containing a collection of pairs where the key is the node name and the value is its content.

Examples

Here is an example:

iex> xml = """
...> <?xml version="1.0" encoding="UTF-8"?>
...> <post id="1">
...>   <title>Hello world!</title>
...>   <stats>
...>     <visits type="integer">1000</visits>
...>     <likes type="integer">3</likes>
...>   </stats>
...> </post>
...> """
iex> XMap.from_xml(xml)
%{"post" => %{"stats" => %{"likes" => "3", "visits" => "1000"},
              "title" => "Hello world!"}}
iex> XMap.from_xml(xml, keys: :atoms)
%{post: %{stats: %{likes: "3", visits: "1000"}, title: "Hello world!"}}

Keys can be converted to atoms with the keys: :atoms option. Unless you absolutely know what you’re doing, do not use the keys: :atoms option. Atoms are not garbage-collected, see Erlang Efficiency Guide for more info:

Atoms are not garbage-collected. Once an atom is created, it will never be removed. The emulator will terminate if the limit for the number of atoms (1048576 by default) is reached.

XML attributes and comments

Both XML attributes and comments are ignored:

iex> xml = """
...> <?xml version="1.0" encoding="UTF-8"?>
...> <post id="1">
...>   <title>Hello world!</title>
...>   <stats>
...>     <visits type="integer">1000</visits>
...>     <likes type="integer">3</likes>
...>   </stats>
...> </post>
...> """
iex> XMap.from_xml(xml, keys: :atoms)
%{post: %{stats: %{likes: "3", visits: "1000"}, title: "Hello world!"}}

Empty XML nodes

Empty XML nodes are parsed as empty maps:

iex> xml = """
...> <?xml version="1.0" encoding="UTF-8"?>
...> <post>
...>   <author/>
...>   <body>Hello world!</body>
...>   <footer></footer>
...> </post>
...> """
iex> XMap.from_xml(xml, keys: :atoms)
%{post: %{author: %{}, body: "Hello world!", footer: %{}}}

Casting

The type casting of the values is delegated to the developer.