1 -module(xml3). 2 -export([main/1]). 3 -include_lib("xmerl/include/xmerl.hrl"). 4 5 parseAll(D) -> 6 % find all RSS files underneath D 7 FL = filelib:fold_files(D, ".+\.rss$", true, fun(F, L) -> [F|L] end, []), 8 [ parse(F) || F <- FL ]. 9 10 parse(FName) -> 11 % parses a single RSS file 12 {R,_} = xmerl_scan:file(FName), 13 ChannelT = hd(xmerl_xpath:string("/rss/channel/title/text()", R)), 14 L = [ process(X) || X <- xmerl_xpath:string("/rss/channel/item", R) ], 15 % print channel title and first two episodes 16 io:format("~n>> ~s~n", [ChannelT#xmlText.value]), 17 io:format("~p~n", [element(1,lists:split(2,L))]), 18 L. 19 20 process(#xmlElement{content = ItemC} = I) -> 21 Data = lists:filter(fun nodef/1, ItemC), 22 [ { element(1, hd(T#xmlText.parents)), T#xmlText.value } || 23 T <- lists:flatten([ xmerl_xpath:string("./text()", C) || C <- Data ]) ]. 24 25 nodef(A) when is_record(A, xmlElement) -> 26 case A#xmlElement.name of 27 title -> true; 28 link -> true; 29 pubDate -> true; 30 _ -> false 31 end; 32 33 nodef(_) -> false. 34 35 % 'main' function (invoked from shell, receives command line arguments) 36 main(A) -> 37 D = atom_to_list(hd(A)), 38 parseAll(D).