tags:

views:

461

answers:

1

Hello -

Can anyone show me how to build a simple XML document using XMERL? The documentation only shows how to append to a 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 out to an XML file:

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

Thanks!

+3  A: 

xmerl's "simple" format is similar to yours: (note the third value, a list of child elements)

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

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

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

Or written to a file:

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

Note that export_simple takes a list of elements, not a single root element. Also, depending on what you do with the result, flattening might be unnecessary.

legoscia
I was missing the list of child elements. Thank you!