Hi richill!

This is what I had in mind. Parse the xml file once building a hash keyed on file name with sub keys giving the before and after links (see the Dump of the hash below).

The File::Find callback sub parses each html file that 'exists' in the lookup and changes the link as appropriate.

The html is written to STDOUT. The file still needs to be written to its final destination.

Hope that helps and good luck :-)

#!/usr/local/bin/perl use strict; use warnings; use XML::Simple; use File::Find; use HTML::TokeParser::Simple; use Data::Dumper; my $dir = "C:/perm/monks/files/html"; my $xmlfile = "C:/perm/monks/xmlfile/urlchange.xml"; my %lookup = get_lookup($xmlfile); find(\&wanted, $dir); sub wanted { my $file = $_; return unless -f $file; my $rel = $File::Find::name; $rel =~ s/$dir//; return unless exists $lookup{$rel}; my $p = HTML::TokeParser::Simple->new($file) or die "couldn't parse $file"; my $new_html; while (my $t = $p->get_token){ if ($t->is_start_tag('a')){ my $href = $t->get_attr('href'); if ($lookup{$rel}{from} = $href){ $t->set_attr('href', $lookup{$rel}{to}); } } $new_html .= $t->as_is; } if (1){ print "$File::Find::name\n"; print "$new_html\n"; print '-' x 20, "\n"; } # todo # write new_html } sub get_lookup{ my ($xmlfile) = @_; my $xml = new XML::Simple(); my $data = $xml->XMLin($xmlfile); my %lookup; for my $sheet (keys %{$data}){ my @records = @{$data->{$sheet}}; for my $record (@records){ $lookup{$record->{OriginPage}} = { from => $record->{LinkToPage}, to => $record->{New_location}, } } } return %lookup; }
Dump of %lookup:
Sheet1 $VAR1 = { '/meeting/series/index.asp' => { 'to' => 'new url', 'from' => 'http://quicklinkurl1/index.asp' }, '/meeting/lunchtime-meeting/index.asp' => { 'to' => 'another changed url', 'from' => 'http://quicklinkurl2/index.asp' }, '/meeting/index.asp' => { 'to' => 'xxxxxxx', 'from' => 'http://quicklinkurl1/index.asp' } };
I created some dummy html files (using file names and links from the xml file) e.g.:
<html> <head> <title>index.asp</title> </head> <body> <a href="http://quicklinkurl1/index.asp">link</a> </body> </html>
output showing that the links have been changed:
C:/perm/monks/files/html/meeting/index.asp <html> <head> <title>index.asp</title> </head> <body> <a href="xxxxxxx">link</a> </body> </html> -------------------- C:/perm/monks/files/html/meeting/lunchtime-meeting/index.asp <html> <head> <title>index.asp</title> </head> <body> <a href="another changed url">link</a> </body> </html> -------------------- C:/perm/monks/files/html/meeting/series/index.asp <html> <head> <title>index.asp</title> </head> <body> <a href="new url">link</a> </body> </html> --------------------
update:
fixed some badly formed html in the dummy files.
update 2:
Note that the last two records in you xml refer to the same page and link but with a different 'new location'. The third record was therefore ignored.


In reply to Re^3: using xml and perl to perform a search and replace on html files by wfsp
in thread using xml and perl to perform a search and replace on html files by richill

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.