in reply to creating an xml file from perl
Try replacing the following two lines and everything below them:
with# open xml file to write open(FIL,">../pub/database.xml");
If the line requesting use HTML::Entities gives you trouble...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";
...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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: creating an xml file from perl
by decerebrate (Initiate) on Apr 14, 2005 at 05:09 UTC |