[erlang-questions] xmerl scan stream

Peter Sabaini <>
Tue Dec 9 10:26:47 CET 2008


On Tuesday 09 December 2008 08:13:38 Taavi Talvik wrote:
> On Dec 9, 2008, at 2:10 AM, Peter Sabaini wrote:
> > Hm, as an afterthought -- this still doesn't solve the original
> > problem, does
> > it?
> >
> > Say I have this on my input stream:
> >
> > % telnet localhost 2345
> > Trying 127.0.0.1...
> > Connected to localhost.local.
> > Escape character is '^]'.
> > <doc>
> > a
> > </doc>
> > <foo />
> >
> > -----
> >
> > then I only get the <doc>a</doc> structure back as soon as <foo />
> > is entered,
> > correct?
>
> I used following continuation functions:


Hi Taavi,

thanks for the code, unfortunately I still have problems -- I can see when an 
XML parse has ended (#xmerl_event), but the scanner still only returns the 
xmlRecord after the next element started? 

Thanks,
peter.

>      {Xml, _Rest} =
>          xmerl_scan:string(Data,
>                            [
>                             {continuation_fun, fun get_more_data/3,
> {started,State#state.socket}},
>                             {event_fun, fun elisa_xml_event/2}])
> ...
>
> %% function to fetch more data
> %% When entity is ended (set in event function), no more data is
> returned
> %% When we are in middle of entiry, then wait for more data
> get_more_data(ContinueF, ExceptionF, GlobalState) ->
>      {Phase,Socket} = xmerl_scan:cont_state(GlobalState),
>      case gen_tcp:recv(Socket, 0, ?DATA_TIMEOUT) of
>          {ok, Data} ->
>              ContinueF(binary_to_list(Data), GlobalState);
>          {error, timeout} ->
>              % if we have somthing started, we need more data
>              % if ended, nothing more is needed
>              case Phase of
>                  started ->
>                      ContinueF([], GlobalState);
>                  ended ->
>                      ExceptionF(GlobalState)
>              end;
>          _Other ->
>              ExceptionF(GlobalState)
>      end.
>
> %%
> %% @doc elisa_xml Event function which sets started|ended flags for
> continuation function.
> elisa_xml_event(#xmerl_event{event=started}, State) ->
>      {_Phase,Socket} = xmerl_scan:cont_state(State),
>      xmerl_scan:cont_state({started,Socket}, State);
> elisa_xml_event(#xmerl_event{event=ended}, State) ->
>      {_Phase,Socket} = xmerl_scan:cont_state(State),
>      xmerl_scan:cont_state({ended,Socket}, State);
> elisa_xml_event(_E, State) ->
>      State.
>
>
> best regards,
> taavi

-- 
  Peter Sabaini
  http://sabaini.at/
  




More information about the erlang-questions mailing list