1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
-module(xmlparser). -author("PC"). -include_lib("xmerl/include/xmerl.hrl"). -include_lib("xmerl/include/xmerl_xpath.hrl"). -include_lib("xmerl/include/xmerl_xsd.hrl"). -include_lib("eunit/include/eunit.hrl"). -export([main/1,get_info/2,search/2]).
main(_v) -> {ok, Req } = file:read_file("c:/erl/soap-master/uri.xml"), Recv=client("239.255.255.250",Req), Fun=fun F([Xml|R])-> io:format("get xml ~n"), {Doc,_} = xmerl_scan:string(binary_to_list(Xml)), get_info(search(Doc,"XAddrs"),[])++F(R); F([])->[] end, io:format("~p~n",[Fun(Recv)]).
client(Port,Msg) -> {ok, Socket} = gen_udp:open(0, [binary]), io:format("client opened socket=~p~n",[Socket]), ok = gen_udp:send(Socket, Port, 3702, Msg ), Value = udp_recieve(Socket,[]),
gen_udp:close(Socket), Value.
udp_recieve(Sock,Value)-> receive {udp, Sock, _, _, Bin} -> udp_recieve(Sock,[Bin]++Value) after 500 -> Value end.
genera_path(Paths,NS)-> lists:flatmap(fun(Item)-> "/"++NS++":"++Item end,Paths).
get_env(Xml_ns,Url) -> List=element(3,Xml_ns), lists:filter(fun(Raw) -> case Raw of {_,Url } -> true; _Else-> false end end, List).
get_info([],Res) -> Res; get_info([Tuple|R],Res)-> case Tuple of #xmlText{parents = Parents,value = Value} -> get_info(R,Res++[{Parents,Value}]); _Else-> Res end.
search(Tuple,Node) when is_tuple(Tuple) -> case Tuple of #xmlElement{nsinfo = {_, Node}} -> #xmlElement{content=Res}=Tuple, Res; #xmlElement{content= Children } -> search(Children,Node,[]); _Else -> [] end. search([],_Node,Res)-> Res; search([Tuple|R],Node,Res) -> search(R,Node,Res ++ search(Tuple,Node)).
|