XMax v1.0.0 XMax View Source
XML to Map conversion.
XMax 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.
XMax was originally forked from XMap. There are 2 notable difference between the packages:
1) XMax will also map xml attributes. This does however cause for larger mapped objects.
Attributes are mapped to the "$" key, while contents are mapped to the "_" key. If you know
you’re never going to need xml attributes, XMap may be a better fit.
2) XMax does not support atom keys, it’s usually not a good idea to generate atoms on the fly. So to prevent unexpected memory leaks, this feature has been ommitted
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> XMax.from_xml(xml)
%{
"blog" => %{
"$" => %{},
"_" => %{
"post" => [
%{
"$" => %{},
"_" => %{"title" => %{"$" => %{}, "_" => "Hello Elixir!"}}
},
%{
"$" => %{},
"_" => %{"title" => %{"$" => %{}, "_" => "Hello World!"}}
}
]
}
}
}
Link to this section Summary
Link to this section Functions
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> XMax.from_xml(xml)
%{
"post" => %{
"$" => %{"id" => '1'},
"_" => %{
"stats" => %{
"$" => %{},
"_" => %{
"likes" => %{"$" => %{"type" => 'integer'}, "_" => "3"},
"visits" => %{"$" => %{"type" => 'integer'}, "_" => "1000"}
}
},
"title" => %{"$" => %{}, "_" => "Hello world!"}
}
}
}
XML attributes and comments
Both XML attributes and comments are mapped as well:
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> XMax.from_xml(xml)
%{
"post" => %{
"$" => %{"id" => '1'},
"_" => %{
"stats" => %{
"$" => %{},
"_" => %{
"likes" => %{"$" => %{"type" => 'integer'}, "_" => "3"},
"visits" => %{"$" => %{"type" => 'integer'}, "_" => "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> XMax.from_xml(xml)
%{
"post" => %{
"$" => %{},
"_" => %{
"author" => %{"$" => %{}, "_" => %{}},
"body" => %{"$" => %{}, "_" => "Hello world!"},
"footer" => %{"$" => %{}, "_" => %{}}
}
}
}
Casting
The type casting of the values is delegated to the developer.