(*
Sample creation of a PXP (an OCaml XML parser) XML tree
Copyright (C) <2003> Stefano Zacchiroli <zack@cs.unibo.it>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
open Pxp_document;;
open Pxp_dtd;;
open Pxp_types;;
open Pxp_yacc;;
(*z an empty DTD *)
let dtd = new dtd default_config.warner default_config.encoding;;
(*z a facility function used to create element nodes.
"name" argument is the name of the elment you would like to create
"attrs" is a list of pairs <attribut_name, attribug_value>
note that element created with this function aren’t validated against the
DTD ( valcheck: false), in order to validate them you will also need to
define a real DTD not a fooish one like above
*)
let create_element name attrs =
create_element_node valcheck:false default_spec dtd name attrs
;;
(*z a facility function used to create data nodes *)
let create_data data = create_data_node default_spec dtd data
;;
(*z create 4 element node, two of them (b1 and c1) with no attributes, two of
them (a1 and a2) with an attribut "att" which have values "apple" and "orange"
respectively *)
let a1 = create_element "a" ["att", "apple"] in
let b1 = create_element "b" [] in
let c1 = create_element "c" [] in
let a2 = create_element "a" ["att", "orange"] in
(*z create 2 data nodes *)
let cherries = create_data "Cherries" in
let orange = create_data "An orange" in
let lemons = create_data "Lemons" in
(*z define the XML tree structure appending nodes to each other *)
a1#append_node b1; (*z append b1 as a child of a1 *)
a1#append_node c1; (*z append c1 as a child of a1 *)
b1#append_node a2; (*z ... and so on *)
b1#append_node cherries;
a2#append_node orange;
c1#append_node lemons;
(*z dump xml output to stdout in the usual (ugly) PXP XML representation, to
pretty print it you can save it to file and use some other pretty printer like
’xmllint’ *)
a1#write (`Out_channel stdout) `Enc_utf8