[ejabberd] Help on XML parse

Kaiduan Xie kaiduanx at gmail.com
Sun Mar 15 06:25:23 MSK 2009


Hi all,

Driven by the interest in Erlang, I started digging into Ejabberd source
code (Learning the best open source Erlang project is a good way to get
familiar with Erlang). I got a problem on XML parsing. What I did was
copying the expat_er.so, and modifying the xml_stream.erl,

kaiduanx at kaiduanx-desktop:~/learn-erl/xml$ ls
expat_erl.c  expat_erl.so  Makefile  xml_stream.beam  xml_stream.erl
kaiduanx at kaiduanx-desktop:~/learn-erl/xml$ erl
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [hipe]
[kernel-poll:false]

Eshell V5.6.5  (abort with ^G)
1> xml_stream:load().
ok
2> Port = xml_stream:new().
#Port<0.411>
3> xml_stream:parse(Port, "<people>Terry</people>").
xml_stream_parse: [{0,{"people",[]}},{2,<<"Terry">>},{1,"people"}]
<<131,108,0,0,0,1,104,2,97,0,104,2,107,0,6,112,101,111,
  112,108,101,106,108,0,0,0,1,104,2,...>>
4> xml_stream:parse(Port, "<people>Terry</people>").
xml_stream_parse: [{3,{9,"junk after document element"}}]
<<131,108,0,0,0,1,104,2,97,3,104,2,97,9,107,0,27,106,117,
  110,107,32,97,102,116,101,114,32,100,...>>
5>

kaiduanx at kaiduanx-desktop:~/learn-erl/xml$ more xml_stream.erl
-module(xml_stream).

-export([load/0,
         new/0,
         parse/2,
         close/1]).

-define(XML_START, 0).
-define(XML_END,   1).
-define(XML_CDATA, 2).
-define(XML_ERROR, 3).

-define(PARSE_COMMAND, 0).
-define(PARSE_FINAL_COMMAND, 1).

load() ->
   case erl_ddll:load_driver(".", "expat_erl") of
       ok -> ok;
       {error, already_loaded} -> ok;
       _ -> exit({error, couldnot_load_driver})
   end.

new() ->
    open_port({spawn, expat_erl}, [binary]).

parse(Port, Str) ->
    Res = port_control(Port, ?PARSE_COMMAND, Str),
    io:format("xml_stream_parse: ~p~n", [binary_to_term(Res)]),
    Res.

close(Port) ->
    port_close(Port).

The problem is: why expat said "junk after document element"? The first
parse succeeded.

Thanks for help,

kaiduan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.jabber.ru/pipermail/ejabberd/attachments/20090314/59f46857/attachment.htm>


More information about the ejabberd mailing list