How to modify an XML file using Perl?

I have a movie collection directory with local links to folders and files for easy access. I recently reorganized all my hard disk space, and I need to update the links, and I'm trying to do this automatically using Perl.

I can export the data to an XML file and import it again. I can extract new file paths using File :: Find , but I ran into two problems. I have no idea how to connect $titlefrom the new path to the file with the corresponding $titlefrom the XML file. I am dealing with such files for the first time, and I do not know how to continue the replacement process. Here is what I have done so far

use strict; 
use warnings; 
use File::Basename;
use File::Find; 
use File::Spec;
use XML::Simple;
use Data::Dumper;



my $dir_target = 'D:/Movies/';
my %titles_locations = ();

find(\&file_handler, $dir_target);
sub file_handler {
   /\.iso$/ or return;       

   my $fn = $File::Find::name;
   $fn =~ s/\//\\/g;
   $fn =~ /(.*\\)(.*)/;
   my $path = $1;
   my $filename = $2;

   my $title = (File::Spec->splitdir($fn))[2];
   $title =~ s/(.*?)\s\(\d+\)$/$1/;
   $title =~ s/~/:/;
   $title =~ s/`/?/;

   my $link_local = '<link><description>Folder</description><url>'.$path.'</url><urltype>Movie</urltype></link><link><description>'.$filename.'</description><url>'.$fn.'</url><urltype>Movie</urltype></link>' unless $title eq '';

   $titles_locations{$title} = {'filename'=>$filename, 'path'=>$path };
}

   my $xml_in = XMLin('somepath/test.xml', ForceArray => 1, KeepRoot => 1);

   my $title = {'key1' => 'title', 'key2' => 'links'};

   foreach my $link (keys %$title) {
   }

   print Data::Dumper->Dump([$title]);

   my $xml_out = XMLout($xml_in, OutputFile => 'somepath/test_out.xml', KeepRoot=>1);       

, . imdb dvdempire - . , , . , . .

<title>$title</title>
.......

<links>
<link>
<description>IMDB</description> 
<url>http://www.imdb.com/title/VARIABLE</url> 
<urltype>URL</urltype> 
</link>
<link>
<description>DVD Empire</description> 
<url>http://www.dvdempire.com/VARIABLE</url> 
<urltype>URL</urltype> 
</link>
<link>
<description>Folder</description>
<url>OLD_FOLDERPATH</url>
<urltype>Movie</urltype>
</link>
<link>
<description>OLD_FILENAME</description>
<url>OLD_FILENAMEPATH</url>
<urltype>Movie</urltype>
</link>
</links>
+3
2

XML:: Simple XML:: Twig, . Twig. , Twig .

, , , . , , , , . , , %new_paths:

#!perl

use File::Basename qw(basename);
use XML::Twig;

my %new_paths = (
         # filename => new_path
         ...
         ); 

my $twig = XML::Twig->new(
    twig_handlers => 
      {
      link   => \&rewrite_link,
      },
    pretty_print => 'indented',
    );

$twig->parse( *DATA );
$twig->flush;

sub rewrite_link
    {
    my( $link ) = $_;

    return unless $link->field( 'urltype' ) eq 'Movie';

    # this is from the old file
    my $basename = basename( $link->field( 'url' ) );

    unless( exists $new_paths{ $basename } )
        {
        warn "Didn't find a new location for $basename!\n";
        return;
        }

    $link->first_child( 'url' )->set_text( $new_paths{ $basename } );
    }

__END__
<titles>
<entry>
    <title>$title</title>
    <links>
        <link>
            <description>IMDB</description> 
            <url>http://www.imdb.com/title/VARIABLE</url> 
            <urltype>URL</urltype> 
        </link>
        <link>
            <description>DVD Empire</description> 
            <url>http://www.dvdempire.com/VARIABLE</url> 
            <urltype>URL</urltype> 
        </link>
        <link>
            <description>Folder</description>
            <url>OLD_FOLDERPATH</url>
            <urltype>Movie</urltype>
        </link>
        <link>
            <description>OLD_FILENAME</description>
            <url>OLD_FILENAMEPATH</url>
            <urltype>Movie</urltype>
        </link>
    </links>
</entry>
</titles>
+3

. , , .

  • my %titles_locations = (); .

  • XML sub a (, , , sub file_handler:)

    :

    • $title $link_local, .

    • %titles_locations $title, , hashref, {'filename'=>$filename, 'path'=>$path }

  • , , find(), XMLin. $xml_in hashrefs ( hashref, "" hashrefs. hashref 1 .

  • .

    ( $title) ref hashref 2 , "title" "links".

    "title" %titles_locations.

    "links" hashref "link" hashrefs. , , , Data::Dumper->Dump([$title]);

    hashrefs. ( $link:

    • $link->{urltype} ne "Movie", (next;)
    • $link->{description} eq "Folder", $link->{url} , %titles_locations.
    • Else, , $link->{url} , %titles_locations.

    , ​​ , $title %titles_locations .

  • $xml_in ( ) XMLout()

DONE

+1

Source: https://habr.com/ru/post/1746952/


All Articles