When I first started looking for XML generation modules, XML::Generator looked like the cleanest, simplest tool for the job: convert a database row (after some data massaging) into an XML document.

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.

The real problem comes when I need to generate the following "looped" section:

<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>
XML::Generator can handle that with a simple looping construct wrapped around the calls, i.e.:
my $imageSection; foreach my $imageId ( 1 .. 4 ) { my $urlName = "imageurl${imageId}"; my $urlCaption = "imagecaption${imageId}"; $imageSection .= $generator->$urlName(), $imageSection .= $generator->$urlCaption(), }
This generated fragment is added to a larger document later, via:
$generator->imageinfo( $imageSection ),

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.

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 N days fighting with the mappings needed with some of the DB columns ... only to 'discover' a better way of generating this later on ...


In reply to nested loops and escaping in XML::Generator by geektron

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.