Skip to content

Instantly share code, notes, and snippets.

@jadeallenx
Forked from nkabardin/README
Created March 7, 2012 16:53
Show Gist options
  • Save jadeallenx/1994346 to your computer and use it in GitHub Desktop.
Save jadeallenx/1994346 to your computer and use it in GitHub Desktop.

Revisions

  1. Mark Allen revised this gist Mar 14, 2012. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions beenzaxml.erl
    Original file line number Diff line number Diff line change
    @@ -28,9 +28,7 @@ process_value(Value, to_binary) -> list_to_binary(Value);
    process_value(Value, _) -> Value.

    element_text_content(#xmlElement{}=Element) ->
    io:format("Element> ~p~n", [Element]),
    Content = Element#xmlElement.content,
    io:format("Content> ~p~n", [Content]),
    case Content of
    [] ->
    undefined;
  2. Mark Allen revised this gist Mar 14, 2012. 2 changed files with 20 additions and 10 deletions.
    8 changes: 7 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,12 @@
    # Purpose

    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 and doesn't ignore the top level tag.
    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

    # Example xml

    22 changes: 13 additions & 9 deletions beenzaxml.erl
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,10 @@
    %% Convert XML to a deep proplist
    %% Turn XML into a deep proplist
    %%
    %% Original code by @wackum
    %%
    %% Modified code to return both attributes and values
    %% Copyright (C) 2012 by Mark Allen. All rights reserved.
    %%
    %% Copyright (C) 2012, Mark Allen
    %% All rights reserved.
    %%
    %% You may use this code under the MIT license as described in the README.
    %% You may use this software under the MIT license as explained in the README

    -module(beenzaxml).
    -export([process_xml/2]).
    @@ -31,9 +28,16 @@ process_value(Value, to_binary) -> list_to_binary(Value);
    process_value(Value, _) -> Value.

    element_text_content(#xmlElement{}=Element) ->
    io:format("Element> ~p~n", [Element]),
    Content = Element#xmlElement.content,
    ContentHead = hd(Content),
    ContentHead#xmlText.value.
    io:format("Content> ~p~n", [Content]),
    case Content of
    [] ->
    undefined;
    _ ->
    ContentHead = hd(Content),
    ContentHead#xmlText.value
    end.

    process_xml(Element, Rules) ->
    TagName = Element#xmlElement.name,
    @@ -46,5 +50,5 @@ process_xml(#xmlElement{name=TagName} = Element, {TagName, _AttributesRules, Chi
    ] ++ State;
    process_xml(#xmlElement{} = Element, Rule, _State) ->
    {_TagName, _AttributeRules, ChildRules} = Rule,
    [{value, process_value(element_text_content(Element), ChildRules)}];
    [{content, process_value(element_text_content(Element), ChildRules)}];
    process_xml(_, _, State) -> State.
  3. Mark Allen revised this gist Mar 13, 2012. 2 changed files with 3 additions and 3 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ test() ->
    Will return easy-to-use deep proplist:

    ```erlang
    {configuration,
    [{configuration,
    [{application,
    [{id,test},
    {platform,
    @@ -89,7 +89,7 @@ Will return easy-to-use deep proplist:
    {package,
    [{points,290},
    {price,5000},
    {id,vk4}]}]}]}]}]}
    {id,vk4}]}]}]}]}]}]

    ```

    2 changes: 1 addition & 1 deletion beenzaxml.erl
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ element_text_content(#xmlElement{}=Element) ->

    process_xml(Element, Rules) ->
    TagName = Element#xmlElement.name,
    {TagName, process_attributes(Element, Rules) ++ process_xml(Element, Rules, [])}.
    [{TagName, process_attributes(Element, Rules) ++ process_xml(Element, Rules, [])}].
    process_xml(#xmlElement{name=TagName} = Element, {TagName, _AttributesRules, ChildrenRules}, State) when is_list(ChildrenRules) ->
    [{ElTag, process_attributes(El, Rule) ++ process_xml(El, Rule, State)} ||
    #xmlElement{name=ElTag} = El <- Element#xmlElement.content,
  4. Mark Allen revised this gist Mar 12, 2012. 2 changed files with 29 additions and 23 deletions.
    39 changes: 21 additions & 18 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Purpose

    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.
    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 and doesn't ignore the top level tag.

    # Example xml

    @@ -70,23 +70,26 @@ test() ->
    Will return easy-to-use deep proplist:

    ```erlang
    [{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}]}]}]}]}]
    {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}]}]}]}]}]}

    ```

    13 changes: 8 additions & 5 deletions beenzaxml.erl
    Original file line number Diff line number Diff line change
    @@ -11,11 +11,12 @@

    -module(beenzaxml).
    -export([process_xml/2]).

    -include_lib("xmerl/include/xmerl.hrl").

    process_attributes(#xmlElement{attributes=Attributes}, Rules) ->
    {_, AR, _} = Rules,
    lists:foldl(fun (Attr, Acc) -> process_attribute(Attr, Acc, AR) end, [], Attributes).
    {_, AttributeRules, _} = Rules,
    lists:foldl(fun (Attr, Acc) -> process_attribute(Attr, Acc, AttributeRules) end, [], Attributes).

    process_attribute(#xmlAttribute{} = Attribute, State, Rules) ->
    Name = Attribute#xmlAttribute.name,
    @@ -34,14 +35,16 @@ element_text_content(#xmlElement{}=Element) ->
    ContentHead = hd(Content),
    ContentHead#xmlText.value.

    process_xml(Element, Rules) -> process_xml(Element, Rules, []).
    process_xml(Element, Rules) ->
    TagName = Element#xmlElement.name,
    {TagName, process_attributes(Element, Rules) ++ process_xml(Element, Rules, [])}.
    process_xml(#xmlElement{name=TagName} = Element, {TagName, _AttributesRules, ChildrenRules}, State) when is_list(ChildrenRules) ->
    [{ElTag, process_attributes(El, Rule) ++ process_xml(El, Rule, State)} ||
    #xmlElement{name=ElTag} = El <- Element#xmlElement.content,
    {RuleTag, _, _}=Rule <- ChildrenRules,
    RuleTag==ElTag
    ] ++ State;
    process_xml(#xmlElement{} = Element, Rule, _State) ->
    {_, _, CR} = Rule,
    [{value, process_value(element_text_content(Element), CR)}];
    {_TagName, _AttributeRules, ChildRules} = Rule,
    [{value, process_value(element_text_content(Element), ChildRules)}];
    process_xml(_, _, State) -> State.
  5. Mark Allen revised this gist Mar 7, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -26,10 +26,10 @@ This module takes XML and turns it into a deep proplist. The original code was
    # Usage example

    ```erlang
    -module(t).
    -export([test/0]).
    -module(t).
    -export([test/0]).

    test() ->
    test() ->
    XmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
    <configuration>
    <application id=\"test\">
  6. Mark Allen revised this gist Mar 7, 2012. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,9 @@ This module takes XML and turns it into a deep proplist. The original code was

    # Example xml

    ```xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration>
    ```xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration>
    <application id="test">
    <platform id="vk">
    <appId>123</appId>
    @@ -20,8 +20,8 @@ This module takes XML and turns it into a deep proplist. The original code was
    </packages>
    </platform>
    </application>
    </configuration>
    ```
    </configuration>
    ```

    # Usage example

  7. Mark Allen revised this gist Mar 7, 2012. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@ This module takes XML and turns it into a deep proplist. The original code was

    # Example xml

    ```xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration>
    <application id="test">
    @@ -20,9 +21,11 @@ This module takes XML and turns it into a deep proplist. The original code was
    </platform>
    </application>
    </configuration>
    ```

    # Usage example
    <pre><code class="erlang">

    ```erlang
    -module(t).
    -export([test/0]).

    @@ -62,11 +65,11 @@ This module takes XML and turns it into a deep proplist. The original code was
    }]
    },
    beenzaxml:process_xml(Element, Rules).
    </code></pre>
    ```

    Will return easy-to-use deep proplist:

    <pre><code class="erlang">
    ```erlang
    [{application,
    [{id,test},
    {platform,
    @@ -85,7 +88,7 @@ Will return easy-to-use deep proplist:
    {package,
    [{points,290},{price,5000},{id,vk4}]}]}]}]}]

    </code></pre>
    ```

    ## LICENSE

  8. Mark Allen revised this gist Mar 7, 2012. 1 changed file with 42 additions and 39 deletions.
    81 changes: 42 additions & 39 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,46 +1,48 @@
    # Purpose

    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.

    # Example xml

    <pre><code class="xml">
    <?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>
    </pre></code>
    <?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>

    # Usage example
    <pre><code class="erlang">
    -module(t).
    -export([test/0]).
    -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>",
    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, [],
    @@ -62,7 +64,7 @@ XmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
    beenzaxml:process_xml(Element, Rules).
    </code></pre>

    # Will return easy-to-use deep proplist:
    Will return easy-to-use deep proplist:

    <pre><code class="erlang">
    [{application,
    @@ -84,7 +86,8 @@ XmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
    [{points,290},{price,5000},{id,vk4}]}]}]}]}]

    </code></pre>
    # LICENSE

    ## LICENSE

    MIT license

  9. Mark Allen revised this gist Mar 7, 2012. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # Example xml

    <pre><code class="xml">
    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration>
    <application id="test">
    @@ -16,8 +17,10 @@
    </platform>
    </application>
    </configuration>
    </pre></code>

    # Usage example
    <pre><code class="erlang">
    -module(t).
    -export([test/0]).

    @@ -57,9 +60,11 @@ XmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
    }]
    },
    beenzaxml:process_xml(Element, Rules).
    </code></pre>

    # Will return easy-to-use deep proplist:

    <pre><code class="erlang">
    [{application,
    [{id,test},
    {platform,
    @@ -78,6 +83,7 @@ XmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
    {package,
    [{points,290},{price,5000},{id,vk4}]}]}]}]}]

    </code></pre>
    # LICENSE

    MIT license
  10. Mark Allen revised this gist Mar 7, 2012. 3 changed files with 110 additions and 68 deletions.
    59 changes: 0 additions & 59 deletions README
    Original file line number Diff line number Diff line change
    @@ -1,59 +0,0 @@
    # Example xml

    <?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>


    # Usage example

    {Element, _} = xmerl_scan:string(binary_to_list(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:

    [{application,
    [{platform,
    [{appId,<<"123">>},
    {secretKey,<<"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}]}]},
    {id,vk}]},
    {id,test}]}]
    91 changes: 91 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    # Example xml

    <?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>

    # Usage example
    -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:

    [{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}]}]}]}]}]

    # LICENSE

    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.
    28 changes: 19 additions & 9 deletions beenzaxml.erl
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,21 @@
    %% Convert XML to a deep proplist
    %%
    %% Original code by @wackum
    %%
    %% Modified code to return both attributes and values
    %%
    %% Copyright (C) 2012, Mark Allen
    %% All rights reserved.
    %%
    %% You may use this code under the MIT license as described in the README.

    -module(beenzaxml).
    -export([process_xml/2]).

    -include_lib("xmerl/include/xmerl.hrl").

    process_attributes(#xmlElement{attributes=Attributes}, Rules) ->
    lists:foldl(fun (Attr, Acc) -> process_attribute(Attr, Acc, Rules) end, [], Attributes).
    {_, AR, _} = Rules,
    lists:foldl(fun (Attr, Acc) -> process_attribute(Attr, Acc, AR) end, [], Attributes).

    process_attribute(#xmlAttribute{} = Attribute, State, Rules) ->
    Name = Attribute#xmlAttribute.name,
    @@ -18,20 +29,19 @@ process_value(Value, to_integer) -> list_to_integer(Value);
    process_value(Value, to_binary) -> list_to_binary(Value);
    process_value(Value, _) -> Value.


    element_text_content(#xmlElement{}=Element) ->
    Content = Element#xmlElement.content,
    ContentHead = hd(Content),
    RawValue = ContentHead#xmlText.value,
    iolist_to_binary(RawValue).
    ContentHead#xmlText.value.

    process_xml(Element, Rules) -> process_xml(Element, Rules, []).
    process_xml(#xmlElement{name=TagName} = Element, {TagName, AttributesRules, ChildrenRules}, State) when is_list(ChildrenRules) ->
    [{ElTag, process_xml(El, Rule, State)} ||
    process_xml(#xmlElement{name=TagName} = Element, {TagName, _AttributesRules, ChildrenRules}, State) when is_list(ChildrenRules) ->
    [{ElTag, process_attributes(El, Rule) ++ process_xml(El, Rule, State)} ||
    #xmlElement{name=ElTag} = El <- Element#xmlElement.content,
    {RuleTag, _, _}=Rule <- ChildrenRules,
    RuleTag==ElTag
    ] ++ process_attributes(Element, AttributesRules) ++ State;
    ] ++ State;
    process_xml(#xmlElement{} = Element, Rule, _State) ->
    process_value(element_text_content(Element), Rule);
    {_, _, CR} = Rule,
    [{value, process_value(element_text_content(Element), CR)}];
    process_xml(_, _, State) -> State.
  11. Nikita Kabardin renamed this gist May 3, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. Nikita Kabardin revised this gist May 3, 2011. 2 changed files with 59 additions and 59 deletions.
    59 changes: 0 additions & 59 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,59 +0,0 @@
    # Example xml

    > <?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>

    # Usage example

    > {Element, _} = xmerl_scan:string(binary_to_list(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:

    > [{application,
    > [{platform,
    > [{appId,<<"123">>},
    > {secretKey,<<"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}]}]},
    > {id,vk}]},
    > {id,test}]}]
    59 changes: 59 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    # Example xml

    <?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>


    # Usage example

    {Element, _} = xmerl_scan:string(binary_to_list(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:

    [{application,
    [{platform,
    [{appId,<<"123">>},
    {secretKey,<<"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}]}]},
    {id,vk}]},
    {id,test}]}]
  13. Nikita Kabardin created this gist May 3, 2011.
    37 changes: 37 additions & 0 deletions beenzaxml.erl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    -module(beenzaxml).
    -export([process_xml/2]).

    -include_lib("xmerl/include/xmerl.hrl").

    process_attributes(#xmlElement{attributes=Attributes}, Rules) ->
    lists:foldl(fun (Attr, Acc) -> process_attribute(Attr, Acc, Rules) end, [], Attributes).

    process_attribute(#xmlAttribute{} = Attribute, State, Rules) ->
    Name = Attribute#xmlAttribute.name,
    Value = Attribute#xmlAttribute.value,
    ProcessedValue = process_value(Value, proplists:get_value(Name, Rules)),
    [{Name, ProcessedValue}|State].

    process_value(Value, Fun) when is_function(Fun ,1) -> Fun(Value);
    process_value(Value, to_atom) -> list_to_atom(Value);
    process_value(Value, to_integer) -> list_to_integer(Value);
    process_value(Value, to_binary) -> list_to_binary(Value);
    process_value(Value, _) -> Value.


    element_text_content(#xmlElement{}=Element) ->
    Content = Element#xmlElement.content,
    ContentHead = hd(Content),
    RawValue = ContentHead#xmlText.value,
    iolist_to_binary(RawValue).

    process_xml(Element, Rules) -> process_xml(Element, Rules, []).
    process_xml(#xmlElement{name=TagName} = Element, {TagName, AttributesRules, ChildrenRules}, State) when is_list(ChildrenRules) ->
    [{ElTag, process_xml(El, Rule, State)} ||
    #xmlElement{name=ElTag} = El <- Element#xmlElement.content,
    {RuleTag, _, _}=Rule <- ChildrenRules,
    RuleTag==ElTag
    ] ++ process_attributes(Element, AttributesRules) ++ State;
    process_xml(#xmlElement{} = Element, Rule, _State) ->
    process_value(element_text_content(Element), Rule);
    process_xml(_, _, State) -> State.
    59 changes: 59 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    # Example xml

    > <?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>

    # Usage example

    > {Element, _} = xmerl_scan:string(binary_to_list(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:

    > [{application,
    > [{platform,
    > [{appId,<<"123">>},
    > {secretKey,<<"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}]}]},
    > {id,vk}]},
    > {id,test}]}]