I haven't used XML::Twig for generating XML, and re-skimming its POD I'm not seeing what must be obvious. Can you show me how it might be used to provide XML matching what this creates?
print $fh <<END;
<event>
<stream-id>$streamid</stream-id>
<event-name>$streamname</event-name>
<primary-event>
<delete-time>$time_t</delete-time>
</primary-event>
</event>
END
It seems to me the OP has control over what he passes through to the XML output. Template systems (even as simple as a HERE doc) seem to fit the bill, but if there's an XML producer that would simplify this further, it would be interesting to see an example of how such a solution looks.
| [reply] [d/l] |
I agree that it's simple thing to produce without using dedicated XML tools, but the character reservations that are involved with XML makes it potentially dangerous to do so. If you're willing to test, capture, and replace the characters (the minimum you should be doing is &, <, >, and %) in the data you're managing, that's fine, but the modules button all of that up for you nicely.
Here's how I'd do that. It's more code, granted, but it's always valid. Given the quality of some of the other tools that I've had to use that require XML, this will give me the best shot at not having to mess with it after I have it in production.
use strict;
use warnings;
use XML::Twig;
my $stream_id = 'stream-id';
my $event_name = 'event-name';
my $time_t = 'time-t';
my $filename = 'foo.xml';
my $twig = XML::Twig->new(pretty_print => 'record');
$twig->parse('<event/>');
my $root = $twig->root();
$root->insert_new_elt('stream-id' => $stream_id);
my $event_tag = $root->insert_new_elt('event-name' => $event_name);
my $primary_event_tag = $event_tag->insert_new_elt('primary-event');
$primary_event_tag->insert_new_elt('delete-time' => $time_t);
open(my $FH, '>', $filename);
$twig->flush(\*$FH);
close $FH;
| [reply] [d/l] [select] |
Thank you for taking the time to demonstrate that. It's a shame there's not a prettier way to do it, but I can appreciate that it is robust and won't try to cram a bunch of dangerous characters into an XML stream. I wouldn't have thought of going to XML::Twig as a producer before. And I can also see that, although producing such a simple stream seems simple, it's possible that untame data can turn it into a disaster.
| [reply] |