[erlang-questions] Record to Xml

Torben Hoffmann torben.lehoff@REDACTED
Wed Jan 7 13:27:39 CET 2009


On Wed, Jan 7, 2009 at 12:04 PM, Edward Stow <ed.stow@REDACTED> wrote:

> 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


 You should have a look at the preprocessor function record_info/2 which
could give you a list of the field names like this:

FieldNames = record_info(fields,ewayRequest),

The problem is that the record name has to be known at compile time so you
need a function along these lines:

record_fields(#evayRequest{}) ->
       record_info(fields,ewayRequest);
record_fields(#other_record{}) ->
       record_info(fields, other_record).

The downside is that you need to add a clause every time you define a new
record.
The record_fields could be auto-generated using a script by keeping all
record definitions in a .hrl file and generate a module with the
record_fields function. Any scripting language will suffice here - I have
used both sed and Erlang for the job in the past.

When you have record_fields/2 in your hands you have to get all the values
out of the record which can be done by mis-using the fact that the internal
representation of a record is a tuple where the first value is the name of
the record.

Then zip the two lists and you are done.

Is this ugly? Yes.
Will it work? Yes - I have it working and live with the pain.

I would also recommend that you take a look at ehtml and how it is used in
yaws. In the yaws_api.erl file you will find a function that translates
ehtml to html and I am pretty sure that function will generate good, clean
XML for you as well.

Cheers,
Torben



>
> 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
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090107/8e412e28/attachment.htm>


More information about the erlang-questions mailing list