perlquestion
geektron
When I first started looking for XML generation modules, [cpan://XML::Generator] looked like the cleanest, simplest tool for the job: convert a database row (after some data massaging) into an XML document.
<p>
Unfortunately, the file I needed to generate has some 250 lines, and my code is almost a method-for-attribute match. While annoying, not the biggest problem with the code.
<p>
The real problem comes when I need to generate the following "looped" section:
<code>
<imageinfo>
<imageurl1>http://www.company.com/images/photo1.jpg</imageurl1>
<imagecaption1>Caption 1</imagecaption1>
<imageurl2>http://www.company.com/images/photo2.jpg</imageurl2>
<imagecaption2>Caption 2</imagecaption2>
<imageurl3>http://www.company.com/images/photo3.jpg</imageurl3>
<imagecaption3>Caption 3</imagecaption3>
<imageurl4>http://www.company.com/images/photo4.jpg</imageurl4>
<imagecaption4>Caption 4</imagecaption4>
</imageinfo>
</code>
XML::Generator can handle that with a simple looping construct wrapped around the calls, i.e.:
<code>
my $imageSection;
foreach my $imageId ( 1 .. 4 ) {
my $urlName = "imageurl${imageId}";
my $urlCaption = "imagecaption${imageId}";
$imageSection .= $generator->$urlName(),
$imageSection .= $generator->$urlCaption(),
}
</code>
This generated fragment is added to a larger document later, via:
<code>
$generator->imageinfo( $imageSection ),
</code>
<p>
The problem comes when trying to avoid the native escaping of data. As expected, I can turn off escaping ... but that's not something I feel I should trust. Two generator objects would *help* -- but the larger part of the generation would be forced to *not* use the escaping.
<p>
I have a feeling there's a much better way of doing this, and I'd rather discover that now before I spend the next <i>N</i> days fighting with the mappings needed with some of the DB columns ... only to 'discover' a better way of generating this later on ...