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 ->
35 let attr_value = Minidom.node_get_attribute_ns parent_node attribute_name uri
39 Printf.printf "found the attribute with ns %s (was %s)\n"
40 (Minidom.string_of_mDOMString attr1) (Minidom.string_of_mDOMString attribute_value)
42 Printf.printf "attribute not found (uri was %s)!!!!\n" (Minidom.string_of_mDOMString uri)
45 Printf.printf "parent_node == NULL || uri == NULL || attribute_name == NULL || attribute_value == NULL\n"
48 let print_attribute attr =
49 check_attribute_ns attr;
50 let ns_uri = Minidom.attr_get_ns_uri attr
54 Some uri -> Printf.printf " %s:" (Minidom.string_of_mDOMString uri);
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)
63 Printf.printf " ??? very strange attribute !!!"
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
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)
77 for i = 1 to n do print_char ' ' done;
81 Some uri -> Printf.printf "%s:" (Minidom.string_of_mDOMString uri)
84 Printf.printf "%s" (Minidom.string_of_mDOMString node_name);
85 List.iter print_attribute attributes;
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)
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
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"
99 Printf.printf "don't know how to manage a node with type %d\n" (Minidom.node_get_type node)