On 17-03-2007, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> Does anyone have any moderate level examples of parsing XML using a
> SAX style parser to build data structures? I'm trying to use Expat to
> do some XML parsing and I just seem to be having a mental block as to
> how to build up a data structure in this style.
>
Here is an example for decoding XML stream with PXP (using SAX API as
far as i know). I am not sure it fits you need, but it is an example...
Let me know if you need more information.
Kind regard
Sylvain Le Gall
let decode exec =
let get_str_opt_attr attr_lst name =
try
Some (List.assoc name attr_lst)
with Not_found ->
None
in
let get_int_opt_attr attr_lst name =
try
Some (int_of_string (List.assoc name attr_lst))
with Not_found | Failure "int_of_string" ->
None
in
let get_float_opt_attr attr_lst name =
try
Some (float_of_string (List.assoc name attr_lst))
with Not_found | Failure "float_of_string" ->
None
in
let get_bool_attr attr_lst name =
try
bool_of_string (List.assoc name attr_lst)
with Not_found->
raise (
DbugXMLInvalid
(Printf.sprintf (f_ "Attribute %s must exist") name)
)
| Failure "bool_of_string" ->
raise (
DbugXMLInvalid
(
Printf.sprintf (f_ "Attribute %s must be true or false, not %s") name
(List.assoc name attr_lst)
)
)
in
let get_int_attr attr_lst name =
try
int_of_string (List.assoc name attr_lst)
with Not_found ->
raise (
DbugXMLInvalid
(Printf.sprintf (f_ "Attribute %s must exist") name)
)
| Failure "int_of_string" ->
raise (
DbugXMLInvalid
(
Printf.sprintf (f_ "Attribute %s must be an integer, not %s") name
(List.assoc name attr_lst)
)
)
in
let decode_attr_informations attr_lst =
{
attr_module = get_str_opt_attr attr_lst "module";
attr_filename = get_str_opt_attr attr_lst "filename";
attr_line = get_int_opt_attr attr_lst "line";
attr_function = get_str_opt_attr attr_lst "function";
attr_tid = get_int_opt_attr attr_lst "tid";
attr_time = get_float_opt_attr attr_lst "time";
}
in
let decode_attr_options attr_lst =
{
attr_output_buffer_length = get_int_attr attr_lst "output_buffer_length";
attr_print_module = get_bool_attr attr_lst "print_module";
attr_print_filename = get_bool_attr attr_lst "print_filename";
attr_print_function = get_bool_attr attr_lst "print_function";
attr_print_line = get_bool_attr attr_lst "print_line";
attr_print_time = get_bool_attr attr_lst "print_time";
attr_print_keyword = get_bool_attr attr_lst "print_keyword";
}
in
let decode_attr_keyword attr_lst =
try
List.assoc "keyword" attr_lst
with Not_found ->
raise (
DbugXMLInvalid (s_ "No keyword in the attribute of a <print>tags")
)
in
let rec decode_print_tag pull_parser buffer =
match pull_parser () with
Some evt ->
(
match evt with
Pxp_types.E_char_data str ->
Buffer.add_string buffer str;
decode_print_tag pull_parser buffer
| Pxp_types.E_end_tag("print",_) ->
Buffer.contents buffer
| Pxp_types.E_end_of_stream ->
raise End_of_file
| _ ->
decode_print_tag pull_parser buffer
)
| None ->
decode_print_tag pull_parser buffer
in
let decode_event pull_parser =
match pull_parser () with
Some evt ->
(
match evt with
Pxp_types.E_start_tag ("dbug",attr,_,_) ->
Some (Start (decode_attr_options attr))
| Pxp_types.E_start_tag ("enter",attr,_,_) ->
Some (Enter (decode_attr_informations attr))
| Pxp_types.E_start_tag ("return",attr,_,_) ->
Some (Return (decode_attr_informations attr))
| Pxp_types.E_start_tag ("print",attr,_,_) ->
let str =
decode_print_tag pull_parser (Buffer.create
DbugConfig.buffer_size)
in
Some (Print (
decode_attr_keyword attr, decode_attr_informations attr, str
)
)
| Pxp_types.E_end_tag("dbug",_) ->
Some Stop
| Pxp_types.E_error exc ->
raise (DbugXMLInvalid (Printexc.to_string exc))
| Pxp_types.E_end_of_stream ->
raise End_of_file
| _ ->
None
)
| None ->
None
in
decode_event exec.pull_parser
;;
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ocaml-developer" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ocaml-developer?hl=en
For other OCaml forums, see http://caml.inria.fr/resources/forums.en.html
-~----------~----~----~----~------~----~------~--~---