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

Bertil Karlsson <>
Mon Nov 6 08:56:16 CET 2006


It is easy to extract xml values first parsing the xml document with 
xmerl_scan:file/[1,2]
and then using xmerl_xpath:string/2 to extract values.

/Bertil

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