<br><div class="gmail_quote">On Wed, Jan 7, 2009 at 12:04 PM, Edward Stow <span dir="ltr"><<a href="mailto:ed.stow@gmail.com">ed.stow@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi<br>
<br>
I am trying to marshal an erlang record to a xml document. In my code<br>
the record field name becomes the element name.<br>
<br>
Is this a reasonable way to produce the Xml.<br>
<br>
I was hoping to be able to "iterate" over the field names (with a list<br>
comprehension perhaps) but I could not<br>
find a way to convert a record to a List in the form</blockquote><div><br> You should have a look at the preprocessor function record_info/2 which could give you a list of the field names like this:<br><br>FieldNames = record_info(fields,ewayRequest),<br>
<br>The problem is that the record name has to be known at compile time so you need a function along these lines:<br><br>record_fields(#evayRequest{}) -><br> record_info(fields,ewayRequest);<br>record_fields(#other_record{}) -><br>
record_info(fields, other_record).<br><br>The downside is that you need to add a clause every time you define a new record.<br>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.<br>
<br>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.<br>
<br>Then zip the two lists and you are done.<br><br>Is this ugly? Yes.<br>Will it work? Yes - I have it working and live with the pain.<br><br>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.<br>
<br>Cheers,<br>Torben<br><br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>
list( {fieldname1, value1}, {fieldname2, value2}).<br>
<br>
My attempts follow:<br>
<br>
-module(eway_xml).<br>
-compile(export_all).<br>
-import(xmerl).<br>
<br>
-record(ewayRequest, {<br>
ewayCustomerID, %required<br>
ewayCustomerFirstName, %required<br>
ewayCustomerLastName, %required<br>
ewayOption1 %optional<br>
}).<br>
<br>
asXml(EwayRecord) -><br>
SimpleContent = asSimpleContent(EwayRecord),<br>
lists:flatten(xmerl:export_simple_content(SimpleContent, xmerl_xml)).<br>
<br>
asSimpleContent(EwayRequest) -><br>
[{ewaygateway,<br>
[{ewayCustomerID, [EwayRequest#ewayRequest.ewayCustomerID]},<br>
{ewayCustomerFirstName ,<br>
[EwayRequest#ewayRequest.ewayCustomerFirstName ]},<br>
{ewayCustomerLastName, [EwayRequest#ewayRequest.ewayCustomerLastName]},<br>
{ewayOption1, [EwayRequest#ewayRequest.ewayOption1]}<br>
]<br>
}].<br>
<br>
testRecord() -><br>
#ewayRequest{<br>
ewayCustomerID = "87654321",<br>
ewayCustomerFirstName = "Jackie",<br>
ewayCustomerLastName = "Chan"<br>
}.<br>
<br>
Running form the command line produces the xml<br>
<br>
12> eway_xml:asXml(eway_xml:testRecord()).<br>
"<ewaygateway><ewayCustomerID>87654321</ewayCustomerID><ewayCustomerFirstName>Jackie</ewayCustomerFirstName><ewayCustomerLastName>Chan</ewayCustomerLastName><ewayOption1><undefined/></ewayOption1></ewaygateway>"<br>
<font color="#888888"><br>
<br>
<br>
<br>
<br>
--<br>
<br>
Edward Stow<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</font></blockquote></div><br>