In that case, here's a working script to demonstrate the basics of what I think you are trying to do.
#perl use strict; use DBI; use XML::LibXML; use Data::Dump 'pp'; my %DB=(); my $dbh = get_dbh(); # connect setup($dbh); # set up database initDB($dbh,\%DB); # fill hash my $parser = XML::LibXML->new(); my @xmlfiles = ('test_1.xml'); for my $xmlfile (@xmlfiles){ print "Parsing $xmlfile\n"; my $doc = readXmlFile($xmlfile); writeXmlFile($doc,'new_'.$xmlfile); } # parse XML sub readXmlFile { my $file = shift; my $doc = $parser->parse_file($file); my @nodes_mt = $doc->findnodes ('/mdc/md/mi/mt'); for my $node_mt (@nodes_mt){ my $elemNameID = $node_mt->getAttribute("p"); # update database if not exists updateDB($dbh,\%DB,$elemNameID) unless exists $DB{$elemNameID}; # translate name to newid my $newID = $DB{$elemNameID}; my @nodes_r = $doc->findnodes ('/mdc/md/mi/r'); for my $node_r (@nodes_r){ my $elemValueID = $node_r->getAttribute("p"); if ($elemNameID eq $elemValueID){ print join " ",$node_mt->textContent,$node_r->textContent,$new +ID,"\n"; # update with newid $node_mt->setAttribute(p => $newID); $node_r->setAttribute(p => $newID); } } } return $doc; } sub writeXmlFile { my ($doc,$outfile) = @_; open my $fh,'>',$outfile or die "Could not open $outfile : $!"; print $fh $doc->toString; close $fh; } pp \%DB; # update db sub updateDB { my ($dbh,$db,$name) = @_; my $sql = 'INSERT INTO XMLelementen VALUES (NULL,?)'; $dbh->do($sql,undef,$name) if ($name); my $sql = 'SELECT id,name FROM XMLelementen WHERE name=?'; my ($id,$name) = $dbh->selectrow_array($sql,undef,$name); $db->{$name} = $id; } # initialize db hash from database sub initDB { my ($dbh,$db) = @_; my $sql = 'SELECT id,name FROM XMLelementen'; my $sth = $dbh->prepare($sql); $sth->execute(); while ( my ($id,$name) = $sth->fetchrow_array ){ $db->{$name} = $id; } } # connect to database sub get_dbh{ my $database = "test"; my $user = "user"; my $pw = "password"; my $dsn = "dbi:mysql:$database:localhost:3306"; my $dbh = DBI->connect($dsn, $user, $pw, { RaiseError=>1, AutoCommit=>1 } ); return $dbh; }; # create database sub setup { $dbh = shift; my $sql = "CREATE TABLE xmlelementen ( id integer NOT NULL AUTO_INCREMENT, name char(255) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; #$dbh->do('DROP TABLE XMLelementen'); $dbh->do($sql); my $sql = 'INSERT IGNORE INTO XMLelementen VALUES (NULL,?)'; $dbh->do($sql,undef,'X'); $dbh->do($sql,undef,'Y'); $dbh->do($sql,undef,'Z'); } __DATA__ <?xml version="1.0"?> <mdc> <md> <mi> <mt p="A1">mt0</mt> <mt p="C1">mt1</mt> <mt p="D1">mt2</mt> <r p="A1">p0</r> <r p="C1">p1</r> <r p="D1">p2</r> </mi> </md> </mdc>
poj

In reply to Re^3: Copy XML file, write new attributes and move the old files. by poj
in thread Copy XML file, write new attributes and move the old files. by Nevamonk

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.