1 (* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
3 * This file is part of mlminidom, the Ocaml binding for minidom.
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.
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.
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.
19 * For details, send a mail to the author.
22 let doc = Minidom.doc_load "test.xml"
24 let root = Minidom.doc_get_root_node doc
26 let check_attribute_ns attr =
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
33 match parent,ns_uri,attr_name,attr_value with
34 Some parent_node,Some uri,Some attribute_name,Some attribute_value ->
36 (Minidom.node_get_attribute_ns parent_node attribute_name uri,
37 Minidom.node_has_attribute_ns parent_node attribute_name uri)
41 Printf.printf "found the attribute with ns %s (was %s)\n"
42 (Minidom.string_of_mDOMString attr1) (Minidom.string_of_mDOMString attribute_value)
44 Printf.printf "attribute not found (uri was %s)!!!!\n" (Minidom.string_of_mDOMString uri)
48 Printf.printf "parent_node == NULL || uri == NULL || attribute_name == NULL || attribute_value == NULL\n"
51 let print_attribute attr =
52 check_attribute_ns attr;
53 let ns_uri = Minidom.attr_get_ns_uri attr
57 Some uri -> Printf.printf " %s:" (Minidom.string_of_mDOMString uri);
60 match ((Minidom.attr_get_name attr), (Minidom.attr_get_value attr)) with
61 (Some attr_name, Some attr_value) ->
62 Printf.printf " %s=\"%s\"" (Minidom.string_of_mDOMString attr_name) (Minidom.string_of_mDOMString attr_value)
63 | (Some attr_name, _) ->
64 Printf.printf " ??? attribute %s has no value !!!" (Minidom.string_of_mDOMString attr_name)
66 Printf.printf " ??? very strange attribute !!!"
69 let rec print_node n node =
70 if Minidom.node_is_blank node then ()
71 else if Minidom.node_is_element node then begin
72 match Minidom.node_get_name node with
75 let children = Minidom.node_get_children node
76 and attributes = Minidom.node_get_attributes node
77 and ns_uri = Minidom.node_get_ns_uri node
78 and is_first,is_last = (Minidom.node_is_first node), (Minidom.node_is_last node)
80 for i = 1 to n do print_char ' ' done;
84 Some uri -> Printf.printf "%s:" (Minidom.string_of_mDOMString uri)
87 Printf.printf "%s" (Minidom.string_of_mDOMString node_name);
88 List.iter print_attribute attributes;
90 List.iter (print_node (n + 2)) children;
91 for i = 1 to n do print_char ' ' done;
92 Printf.printf "</%s>\n" (Minidom.string_of_mDOMString node_name)
94 | None -> Printf.printf "??? this node has no name !!!\n"
95 end else if Minidom.node_is_text node then begin
96 match Minidom.node_get_content node with
98 for i = 1 to n do print_char ' ' done;
99 Printf.printf "%s\n" (Minidom.string_of_mDOMString node_content)
100 | None -> Printf.printf "??? this node has no content !!!\n"
102 Printf.printf "don't know how to manage a node with type %d\n" (Minidom.node_get_type node)