]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mlminidom/test.ml
Version 1.2.1beta => 1.2.1
[helm.git] / helm / DEVEL / mlminidom / test.ml
1 (* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
2  *
3  * This file is part of mlminidom, the Ocaml binding for minidom.
4  * 
5  * mlminidom is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * mlminidom is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with mlminidom; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  * 
19  * For details, send a mail to the author.
20  *)
21
22 let doc = Minidom.doc_load "test.xml"
23
24 let root = Minidom.doc_get_root_node doc
25
26 let check_attribute_ns attr =
27   Printf.printf "\n\n";
28   let ns_uri = Minidom.attr_get_ns_uri attr
29   and attr_name = Minidom.attr_get_name attr
30   and attr_value = Minidom.attr_get_value attr
31   and parent = Minidom.attr_get_parent attr
32   in
33   match parent,ns_uri,attr_name,attr_value with
34     Some parent_node,Some uri,Some attribute_name,Some attribute_value ->
35       let attr_value = Minidom.node_get_attribute_ns parent_node attribute_name uri
36       in begin
37         match attr_value with
38           Some attr1 ->
39             Printf.printf "found the attribute with ns %s (was %s)\n"
40               (Minidom.string_of_mDOMString attr1) (Minidom.string_of_mDOMString attribute_value)
41         | None ->
42             Printf.printf "attribute not found (uri was %s)!!!!\n" (Minidom.string_of_mDOMString uri)
43       end
44   | _ ->
45       Printf.printf "parent_node == NULL || uri == NULL || attribute_name == NULL || attribute_value == NULL\n"
46 ;;
47     
48 let print_attribute attr =
49   check_attribute_ns attr;
50   let ns_uri = Minidom.attr_get_ns_uri attr
51   in
52   begin
53     match ns_uri with
54       Some uri -> Printf.printf " %s:" (Minidom.string_of_mDOMString uri);
55     | None -> ()
56   end;
57   match ((Minidom.attr_get_name attr), (Minidom.attr_get_value attr)) with
58     (Some attr_name, Some attr_value) ->
59       Printf.printf " %s=\"%s\"" (Minidom.string_of_mDOMString attr_name) (Minidom.string_of_mDOMString attr_value) 
60   | (Some attr_name, _) ->
61       Printf.printf " ??? attribute %s has no value !!!" (Minidom.string_of_mDOMString attr_name)
62   | (_,_) ->
63       Printf.printf " ??? very strange attribute !!!"
64 ;;
65
66 let rec print_node n node =
67   if Minidom.node_is_blank node then ()
68   else if Minidom.node_is_element node then begin
69     match Minidom.node_get_name node with
70       Some node_name -> 
71         begin
72           let children = Minidom.node_get_children node
73           and attributes = Minidom.node_get_attributes node
74           and ns_uri = Minidom.node_get_ns_uri node
75           and is_first,is_last = (Minidom.node_is_first node), (Minidom.node_is_last node)
76           in
77           for i = 1 to n do print_char ' ' done;
78           Printf.printf "<";
79           begin
80             match ns_uri with
81               Some uri -> Printf.printf "%s:" (Minidom.string_of_mDOMString uri)
82             | None     -> ()
83           end;
84           Printf.printf "%s" (Minidom.string_of_mDOMString node_name);
85           List.iter print_attribute attributes;
86           Printf.printf ">\n";
87           List.iter (print_node (n + 2)) children;
88           for i = 1 to n do print_char ' ' done;
89           Printf.printf "</%s>\n" (Minidom.string_of_mDOMString node_name)
90         end
91     | None -> Printf.printf "??? this node has no name !!!\n"
92   end else if Minidom.node_is_text node then begin
93     match Minidom.node_get_content node with
94       Some node_content ->
95         for i = 1 to n do print_char ' ' done;
96         Printf.printf "%s\n" (Minidom.string_of_mDOMString node_content)
97     | None -> Printf.printf "??? this node has no content !!!\n"
98   end else begin
99     Printf.printf "don't know how to manage a node with type %d\n" (Minidom.node_get_type node)
100   end
101 ;;
102   
103 print_node 0 root;;
104