G
gabkin
I am using XML::Simple to get some metadata from an XML file, and I
usually access the metadata like this:
$metadata{Label} = $XML->{LabelName};
$metadata{'Copyright Holder'} = $XML->{RightsHolder};
$metadata{'Release Data'} = $XML->{OriginalReleaseDate};
$metadata{'Product ID'} = $XML->{ProductId};
$metadata{'Thumbnail file'} =
$XML->{DigitalFiles}->{CoverFile}->{FileInfo}->{FileName};
etc.
(This XML file really is overkill, and tends to obfuscate the data, but
it works, and its what I've got.)
But for 'Artist', I have an issue, sometimes XML::Simple gives a hash
for 'Artists', and sometimes it gives a list, with a separate hash
inside the list for each artist. This seems to occur when multiple
artists collaborate on a track/album.
Perhaps my code will illustrate what Im trying to achieve...
# there may be multiple artists, if there are,
# there is a list in 'Artist', followed by a hash:
# otherwise there is just a hash in artist.
# the eval trick will detect whether there is a list there or not,
# and then act accordingly
eval{my $foo = $XML->{Artists}->{Artist}->[0]};
if ($@){ # this will trigger if the above 'look for a list' code bombs
$metadata{Artist} = $XML->{Artists}->{Artist}->{FullName};
} else { # this means there must be multiple artists
my $comboArtistName;
my $Artists = $XML->{Artists}->{Artist};
my $size = @$Artists;
for(my $i=0;$i<$size;$i++){
$comboArtistName.=" & " if $comboArtistName;
$comboArtistName.=$XML->{Artists}->{Artist}->[$i]->{FullName};
}
$metadata{Artist}=$comboArtistName;
} # there must be a better way to do the above
Anyway, I have tested this, and it works, but it seems rather ungainly.
I cant think of another better way of doing this, but I was hoping that
someone else here could...
usually access the metadata like this:
$metadata{Label} = $XML->{LabelName};
$metadata{'Copyright Holder'} = $XML->{RightsHolder};
$metadata{'Release Data'} = $XML->{OriginalReleaseDate};
$metadata{'Product ID'} = $XML->{ProductId};
$metadata{'Thumbnail file'} =
$XML->{DigitalFiles}->{CoverFile}->{FileInfo}->{FileName};
etc.
(This XML file really is overkill, and tends to obfuscate the data, but
it works, and its what I've got.)
But for 'Artist', I have an issue, sometimes XML::Simple gives a hash
for 'Artists', and sometimes it gives a list, with a separate hash
inside the list for each artist. This seems to occur when multiple
artists collaborate on a track/album.
Perhaps my code will illustrate what Im trying to achieve...
# there may be multiple artists, if there are,
# there is a list in 'Artist', followed by a hash:
# otherwise there is just a hash in artist.
# the eval trick will detect whether there is a list there or not,
# and then act accordingly
eval{my $foo = $XML->{Artists}->{Artist}->[0]};
if ($@){ # this will trigger if the above 'look for a list' code bombs
$metadata{Artist} = $XML->{Artists}->{Artist}->{FullName};
} else { # this means there must be multiple artists
my $comboArtistName;
my $Artists = $XML->{Artists}->{Artist};
my $size = @$Artists;
for(my $i=0;$i<$size;$i++){
$comboArtistName.=" & " if $comboArtistName;
$comboArtistName.=$XML->{Artists}->{Artist}->[$i]->{FullName};
}
$metadata{Artist}=$comboArtistName;
} # there must be a better way to do the above
Anyway, I have tested this, and it works, but it seems rather ungainly.
I cant think of another better way of doing this, but I was hoping that
someone else here could...