This module takes XML and turns it into a deep proplist. The original code was written by @wackum.
I modified it so it:
- returns both attributes and the value instead of one or the other
- doesn't ignore the top level tag
- handles elements with empty content
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<application id="test">
<platform id="vk">
<appId>123</appId>
<secretKey>secret</secretKey>
<packages>
<package id="vk0" price="100" points="3" />
<package id="vk1" price="500" points="20" />
<package id="vk2" price="1000" points="45" default="true" />
<package id="vk3" price="2500" points="130" />
<package id="vk4" price="5000" points="290" />
</packages>
</platform>
</application>
</configuration>
-module(t).
-export([test/0]).
test() ->
XmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<configuration>
<application id=\"test\">
<platform id=\"vk\">
<appId>123</appId>
<secretKey>secret</secretKey>
<packages>
<package id=\"vk0\" price=\"100\" points=\"3\" />
<package id=\"vk1\" price=\"500\" points=\"20\" />
<package id=\"vk2\" price=\"1000\" points=\"45\" default=\"true\" />
<package id=\"vk3\" price=\"2500\" points=\"130\" />
<package id=\"vk4\" price=\"5000\" points=\"290\" />
</packages>
</platform>
</application>
</configuration>",
{Element, _} = xmerl_scan:string(XmlData),
Rules = {configuration, [],
[{application, [{id, to_atom}],
[{platform, [{id, to_atom}],
[
{appId, [], to_binary},
{secretKey, [], to_binary},
{packages, [], [
{package, [{id, to_atom},
{price, to_integer},
{points, to_integer},
{default, to_atom}], []}
]}
]
}]
}]
},
beenzaxml:process_xml(Element, Rules).
Will return easy-to-use deep proplist:
[{configuration,
[{application,
[{id,test},
{platform,
[{id,vk},
{appId,[{value,<<"123">>}]},
{secretKey,[{value,<<"secret">>}]},
{packages,
[{package,[{points,3},{price,100},{id,vk0}]},
{package,[{points,20},{price,500},{id,vk1}]},
{package,
[{default,true},
{points,45},
{price,1000},
{id,vk2}]},
{package,[{points,130},{price,2500},{id,vk3}]},
{package,
[{points,290},
{price,5000},
{id,vk4}]}]}]}]}]}]
MIT license
Copyright (c) 2012 Mark Allen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.