-module(djimport). -export([start/0]). -include_lib("/usr/local/lib/erlang/lib/xmerl-1.1.4/include/xmerl.hrl"). % Definition of a 'record' record. How confusing. -record(record, {id, filename, lastplayed, numplays, rating, title, artist, notes}). start() -> % Create schema, start Mnesia and create the record table... mnesia:create_schema([node()]), mnesia:start(), mnesia:create_table(record, [{disc_copies, [node()]}, {attributes, record_info(fields, record)}]), % Parse the document and add record records to the table... { Xml, _Rest } = xmerl_scan:file("../media/LwDJState.xml"), Records = xmerl_xpath:string("/lwdj/record",Xml), process_record(Records). % Process a list of records... process_record([]) -> ok; process_record([Head|Rest]) -> io:format("Processing record~n",[]), New_rec=#record{}, process_record_attrs(Head#xmlElement.attributes,New_rec), process_record(Rest). % Process attributes for a record... process_record_attrs([],New_rec) -> Fun = fun() -> mnesia:write(New_rec) end, mnesia:transaction(Fun); process_record_attrs([Head|Rest],New_rec) -> io:format(" ~s = ~s~n",[Head#xmlAttribute.name,Head#xmlAttribute.value]), Fieldname=Head#xmlAttribute.name, case Fieldname of id -> Updated_rec=New_rec#record{id=Head#xmlAttribute.value}; filename -> Updated_rec=New_rec#record{filename=Head#xmlAttribute.value}; lastplayed -> Updated_rec=New_rec#record{lastplayed=Head#xmlAttribute.value}; numplays -> Updated_rec=New_rec#record{numplays=Head#xmlAttribute.value}; rating -> Updated_rec=New_rec#record{rating=Head#xmlAttribute.value}; title -> Updated_rec=New_rec#record{title=Head#xmlAttribute.value}; artist -> Updated_rec=New_rec#record{artist=Head#xmlAttribute.value}; notes -> Updated_rec=New_rec#record{notes=Head#xmlAttribute.value} end, process_record_attrs(Rest,Updated_rec).