Skip to content

Instantly share code, notes, and snippets.

@cisiqo
Created February 18, 2023 12:37
Show Gist options
  • Save cisiqo/df6c1a59af3f12df0e15a5e7c71b6b5b to your computer and use it in GitHub Desktop.
Save cisiqo/df6c1a59af3f12df0e15a5e7c71b6b5b to your computer and use it in GitHub Desktop.
erlang xmerl
-module(test).
-export([start/0, start1/0, start2/0]).
-include_lib("xmerl/include/xmerl.hrl").
make_doc_xml(Fields) ->
Xml = xmerl:export_simple([doc_xml_simple(Fields)], xmerl_xml,
[{prolog, ""}]),
unicode:characters_to_binary(Xml).
doc_xml_simple(Fields) ->
{add, [{doc, fields_to_xml_simple(Fields)}]}.
fields_to_xml_simple(Fields) ->
[ {field, [{name, K}], [V]} || {K, V} <- Fields ].
start() ->
Fields = [{"id", "9885A004"},
{"name", "Canon PowerShot SD500"},
{"category", "camera"},
{"features", "3x optical zoom"},
{"features", "aluminum case"},
{"weight", "6.4"},
{"price", "329.95"}
],
make_doc_xml(Fields).
start1() ->
Fields = {'FolderSync', [{'Status', ["1"]}, {'SyncKey', ["1"]}, {'Changes', [{'Count', ["0"]}]}]},
Xml = xmerl:export_simple([Fields], xmerl_xml, [{prolog, ""}]),
unicode:characters_to_binary(Xml).
start2() ->
{Element, _} = xmerl_scan:string("<FolderSync><Status>1</Status><SyncKey>1</SyncKey><Changes><Count>0</Count></Changes></FolderSync>",
[{space, normalize}]),
[Clean] = xmerl_lib:remove_whitespace([Element]),
xmerl_lib:simplify_element(Clean).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment