[erlang-questions] Iteration over records/record fields ?

Willem de Jong <>
Sun Nov 5 19:32:18 CET 2006


Hi Igwan,

If I understand correctly, you could solve your problem by using
Erlsom (http://sourceforge.net/projects/erlsom).

You have to define an XML schema (XSD) for the XML. Normally that
shouldn't be very difficult. On the basis of this schema, Erlsom can
parse the XML. The output is a record (or a nested structure of
records, depending on the structure of the XML).

Good luck,
Willem.


On 11/5/06, igwan <> wrote:
> Hi,
>
> For a project of mine, I have to import some XML elements into a bunch
> of Mnesia tables. Each record I defined map exactly to the structure of
> each XML element.
>
> Example :
>
> XML :
> <person>
>  <name>Simpson</name>
>  <firstname>Homer J.</firstname>
>  ...
> </person>
>
> <house>
>  <address>742 Evergreen T.</address>
>  <coordinates>42.293,12.942,0<coordinates>
>  ...
> </house>
>
> after parsing and transformation, I get a list like :
> [{person, [{name, "Simpson"}, {firstname, "Homer J."}, ... ]},
> {house, [{address, "742 Evergreen T."}, {coordinates,
> "42.293,12.942,0"}, ...],
> ... ]
>
> The corresponding record I define :
> -record(person, { name = [], firstname = [], ... }).
> -record(house, { address = [], coordinates = [], .... }).
> ...
>
> The problem is that I have an important list of records defined (about
> 30), each having about 20 to 40 fields.
> How can I programmatically iterate over my list  to populate my records ?
>
> I wish I could write #RecordName.Field or record_info(fields,
> RecordName), with RecordName and Field being variables, and iterate in
> list comprehensions and lists:foldl but that doesn't seem possible. Is
> there a way around ? (I wish I could also avoid macros. ^^). I also
> heard of the Smerl trickery, is that the way to go ?
>
> Thanks
>
> igwan
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list