[erlang-questions] Record to Xml

Edward Stow <>
Wed Jan 7 12:04:54 CET 2009


Hi

I am trying to marshal an erlang record to a xml document.  In my code
the record field name becomes the element name.

Is this a reasonable way to produce the Xml.

I was hoping to be able to "iterate" over the field names (with a list
comprehension perhaps) but I could not
find a way to convert a record to a List in the form

list( {fieldname1, value1},  {fieldname2, value2}).

My attempts follow:

-module(eway_xml).
-compile(export_all).
-import(xmerl).

-record(ewayRequest, {
    ewayCustomerID,             %required
    ewayCustomerFirstName,   %required
    ewayCustomerLastName,    %required
    ewayOption1                     %optional
}).

asXml(EwayRecord) ->
    SimpleContent = asSimpleContent(EwayRecord),
    lists:flatten(xmerl:export_simple_content(SimpleContent, xmerl_xml)).

asSimpleContent(EwayRequest) ->
    [{ewaygateway,
        [{ewayCustomerID, [EwayRequest#ewayRequest.ewayCustomerID]},
        {ewayCustomerFirstName ,
[EwayRequest#ewayRequest.ewayCustomerFirstName ]},
        {ewayCustomerLastName, [EwayRequest#ewayRequest.ewayCustomerLastName]},
        {ewayOption1, [EwayRequest#ewayRequest.ewayOption1]}
        ]
    }].

testRecord() ->
    #ewayRequest{
        ewayCustomerID = "87654321",
        ewayCustomerFirstName = "Jackie",
        ewayCustomerLastName = "Chan"
    }.

Running form the command line produces the xml

12> eway_xml:asXml(eway_xml:testRecord()).
"<ewaygateway><ewayCustomerID>87654321</ewayCustomerID><ewayCustomerFirstName>Jackie</ewayCustomerFirstName><ewayCustomerLastName>Chan</ewayCustomerLastName><ewayOption1><undefined/></ewayOption1></ewaygateway>"





-- 

Edward Stow



More information about the erlang-questions mailing list