[erlang-questions] xmerl scan stream

Peter Sabaini <>
Mon Dec 8 21:38:59 CET 2008

Hi list,

I am trying to get xmerl to parse a stream of data coming in via a TCP socket. 
The goal would be for xmerl to return xmlRecords as soon as one is complete.

I use the continuation function option of xmerl and so far that works ok; 
unfortunately I only get an xmlRecord as soon as the next xml element starts. 
Is there a way to tell xmerl to "evaluate eagerly"?

Below is the test code I used; any help much appreciated. Is this even 
possible? Or am I completely on the wrong track and should use a SAX model 

 -- snip --


start_server() ->
    {ok, Listen} = gen_tcp:listen(2345, [binary, {packet, raw},
					 {reuseaddr, true},
					 {active, true}]),
    spawn(fun() -> par_connect(Listen) end).

par_connect(Listen) ->
    {ok, _Socket} = gen_tcp:accept(Listen),
    spawn(fun() -> par_connect(Listen) end),
    io:format("par_c ~n", []),
    X = xmerl_scan:string("", [{continuation_fun, fun continue/3}]),
    io:format("X: ~p ~n", [X]).

continue(Continue, Exception, GlobalState) ->
    io:format("entered continue/3 ~n", []),
	{tcp, _Socket, Bin} ->    
	    Str = binary_to_list(Bin),
	    io:format("got Str ~p ~n", [Str]),
	    Continue(Str, GlobalState);
	{tcp_closed, _} ->
	    io:format("Server socket closed~n" ),

main() ->

 -- snip --

  Peter Sabaini

More information about the erlang-questions mailing list