Try replacing the following two lines and everything below them:

# open xml file to write open(FIL,">../pub/database.xml");
with
my $img_dir = '../images'; opendir HOMEDIR, $img_dir or die "opendir failed: $!\n"; my @filenames = readdir HOMEDIR; closedir HOMEDIR or die "closedir failed: $!\n"; open FIL, '>', '../pub/database.xml' or die "open failed: $!\n"; flock( FIL, 2 ) or die "flock failed: $!\n"; print FIL <<EOXML; <?xml version="1.0"?> <site> EOXML { my $path_to_imgs = '/images'; my @f = @filenames; # @f will be destroyed my $t = 1; use HTML::Entities; for my $filename ( map encode_entities( $_ ), grep s/\.jpg$//i, @f ) { print FIL qq(<item id="$t" imageURL="$path_to_imgs/$filename" />\n +); $t++; } } print FIL "</site>\n"; flock FIL, 8 or die "flock failed: $!\n"; close FIL or die "close failed: $!\n";
If the line requesting use HTML::Entities gives you trouble...

...comment it out, and add the following definition to the file:

{ my ( $re, %c2e, $num_ent ); INIT { %c2e = ( '&' => 'amp' , '>' => 'gt' , '<' => 'lt' , '"' => 'quot', "'" => 'apos', ); $re = qr/[^\n\r\t !\#\$%\(-;=?-~]/; $num_ent = sub { sprintf '#x%X', ord( $_[ 0 ] ) }; } sub encode_entities { my $string = shift; $string =~ s/($re)/sprintf '&%s;', $c2e{$1} || $num_ent->( $1 )/ge +; return $string; } }

Update: Fixed closedir bug; added entities encoding (following ikegami's suggestion).

the lowliest monk


In reply to Re: creating an xml file from perl by tlm
in thread creating an xml file from perl by decerebrate

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.