Creating an XMERL document in Erlang

Can someone show me how to create a simple XML document using XMERL? The documentation only shows how to add to the current XML document that is read from a file. I want to create a new XML document from scratch.

For example, I want to write a simple structure like this to an XML file:

Data = {myNode,[{foo,"Foo"},{bar,"Bar"}]}. 

Thanks!

+6
xml erlang
source share
1 answer

xmerl "simple" format is similar to yours: (pay attention to the third value, the list of child elements)

 Data = {myNode,[{foo,"Foo"},{bar,"Bar"}], []}. 

This can be "exported" to XML for use as a string:

 > lists:flatten(xmerl:export_simple([Data], xmerl_xml)). "<?xml version=\"1.0\"?><myNode foo=\"Foo\" bar=\"Bar\"/>" 

Or is written to the file:

 > file:write_file("/tmp/foo.xml", xmerl:export_simple([Data2], xmerl_xml)). ok 

Note that export_simple accepts a list of elements, not a single root element. Also, depending on what you do with the result, alignment may not be necessary.

+8
source share

All Articles