+# create the 'Message' subtree
+#
+# Params: $xml - XML::DOM::Document object
+# $par - hash reference
+# (msg, ip, name, email, home, image, category, subject, time)
+#
+# Return: XML::DOM::Element object
+#
+sub create_message ($$) {
+ my ($xml,$par) = @_;
+
+ my $message = $xml -> createElement ('Message');
+ $message -> setAttribute ('id' => $par -> {msg});
+ $message -> setAttribute ('ip' => $par -> {ip});
+
+ my $header = $xml -> createElement ('Header');
+ my $author = $xml -> createElement ('Author');
+ $header -> appendChild ($author);
+
+ my @may = (
+ ['name' => 'Name' => $author],
+ ['email' => 'Email' => $author],
+ ['home' => 'HomepageUrl' => $author],
+ ['image' => 'ImageUrl' => $author],
+ ['category' => 'Category' => $header],
+ ['subject' => 'Subject' => $header]
+ );# key => element name => superior
+
+ for (@may) {
+
+ # create element
+ my $obj = $xml -> createElement ($_->[1]);
+
+ # insert content
+ $obj -> addText (
+ defined $par -> {$_->[0]}
+ ? $par -> {$_->[0]}
+ : ''
+ );
+
+ # link to superior element
+ $_ -> [2] -> appendChild ($obj);
+ }
+
+ my $date = $xml -> createElement ('Date');
+ $date -> setAttribute ('longSec'=> $par -> {time});
+
+ $header -> appendChild ($date);
+ $message -> appendChild ($header);
+
+ # return
+ #
+ $message;
+}
+
+### sub create_new_thread ($) ##################################################
+#
+# create a XML::DOM::Document object of a thread containing one posting
+#
+# Params: hash reference
+# (dtd, thread, msg, body, ip, name, email, home,
+# image, category, subject, time)
+#
+# Return: XML::DOM::Document object
+#
+sub create_new_thread ($) {
+ my $par = shift;
+
+ # new document
+ #
+ my $xml = new XML::DOM::Document;
+
+ # xml declaration
+ #
+ my $decl = new XML::DOM::XMLDecl;
+ $decl -> setVersion ('1.0');
+ $decl -> setEncoding ('UTF-8');
+ $xml -> setXMLDecl ($decl);
+
+ # set doctype
+ #
+ my $dtd = $xml -> createDocumentType ('Forum' => $par -> {dtd});
+ $xml -> setDoctype ($dtd);
+
+ # create root element 'Forum'
+ # create element 'Thread'
+ # create 'Message' subtree
+ # create element 'ContentList'
+ # create 'MessageContent' subtree
+ #
+ my $forum = $xml -> createElement ('Forum');
+ my $thread = $xml -> createElement ('Thread');
+ $thread -> setAttribute ('id' => $par -> {thread});
+ my $message = create_message ($xml,$par);
+ my $content = $xml -> createElement ('ContentList');
+ my $mcontent = $xml -> createElement ('MessageContent');
+ $mcontent -> setAttribute ('mid' => $par -> {msg});
+ $mcontent -> appendChild (
+ $xml -> createCDATASection (${$par -> {body}})
+ );
+
+ # link all the nodes to
+ # their superior elements
+ #
+ $thread -> appendChild ($message);
+ $forum -> appendChild ($thread);
+ $content -> appendChild ($mcontent);
+ $forum -> appendChild ($content);
+ $xml -> appendChild ($forum);
+
+ # return
+ #
+ $xml;
+}