Skip to content

Instantly share code, notes, and snippets.

@refactor
Last active January 21, 2019 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save refactor/21926e2b9dda1857ef7e6b55b2389142 to your computer and use it in GitHub Desktop.
Save refactor/21926e2b9dda1857ef7e6b55b2389142 to your computer and use it in GitHub Desktop.
try to modify xml, add some element node
-include_lib("xmerl/include/xmerl.hrl").
%% tuple -> xml-erl
TL = {filemeta,[{id,'/Foo'},{bar,12111111113}], [{name,["11"]},{age,["1"]}]}.
XmlDoc = xmerl:export_simple([TL], xmerl_xml).
%% xml-erl -> tuple
TL = xmerl_lib:simplify_element(XmlDoc).
% xml-file -> tuple
{XmlDoc,_} = xmerl_scan:file("my-dem.vrt").
TL = xmerl_lib:simplify_element(XmlDoc).
% xml-string -> tuple
{XmlDoc, _} = xmerl_scan:string(Str).
TL = xmerl_lib:simplify_element(XmlDoc).
% parse xml node... but it's complex, I'd rather parsing tuple
%% shell> rr(code:lib_dir(xmerl) ++ "/include/xmerl.hrl").
XmlDoc#xmlElement.content.
NewTL = work_on(TL).
% tuple -> xml-string
lists:flatten(xmerl:export_simple([NewTL], xmerl_xml))).
% tuple -> xml-string
% rm header: "<?xml version=\"1.0\"?>"
lists:flatten(tl(xmerl:export_simple([NewTL], xmerl_xml))).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment