-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_xml.erl
56 lines (40 loc) · 1.4 KB
/
test_xml.erl
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
-module(test_xml).
-export([parse_file/1]).
-behaviour(simplexml).
-export([start_document/2, end_document/2, start_element/4, end_element/3,
characters/3]).
parse_file(Filename) ->
encodings:start(),
{ok, File} = file:open(Filename, [read, raw, binary]),
{continue, ParseState} = simplexml:parse(<<>>, Filename, ?MODULE, none),
read_file(File, ParseState),
encodings:stop().
read_file(File, ParseState) ->
case file:read(File, 4096) of
{ok, Data} ->
case simplexml:parse(Data, ParseState) of
{continue, NewParseState} ->
read_file(File, NewParseState);
{ok, _} ->
file:close(File)
end;
eof ->
simplexml:parse(eof, ParseState),
file:close(File)
end.
start_document(Location, Args) ->
io:format("start document (~p)~n", [Location]),
{ok, Args}.
end_document(Location, State) ->
io:format("end document (~p)~n", [Location]),
{ok, State}.
start_element(Tag, Attributes, Location, State) ->
io:format("start element (~p): ~p ~p~n", [Location, Tag, Attributes]),
{ok, State}.
end_element(Tag, Location, State) ->
io:format("end element (~p): ~p~n", [Location, Tag]),
{ok, State}.
characters(Chunk, Location, State) ->
io:format("characters (~p): ~ts~n",
[Location, encodings:encode(Chunk, utf8)]),
{ok, State}.