in reply to Update XML Values using two primary keys
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Text::CSV_XS; use XML::LibXML; my %name; my $csv = 'Text::CSV_XS'->new({allow_whitespace => 1, auto_diag => 1, binary => 1}); open my $in, '<:encoding(UTF-8)', 'test.csv' or die $!; $csv->header($in); while (my $row = $csv->getline($in)) { $name{$row->[0]}{$row->[1]} = $row->[2]; } my $dom = 'XML::LibXML'->load_xml(location => 'test.osm'); for my $node ($dom->findnodes('/osm/node')) { my $tag = $node->findnodes('tag[@k="name"]')->[0]; if (my $name = $name{ $node->{lat} }{ $node->{lon} }) { $tag->{v} = $name; } else { warn "No name found for $tag->{v}: @$node{qw{ lat lon }}\n"; } } $dom->toFile('new.osm');
Or, you can load the XML into a DOM, then read the CSV line by line, searching the DOM for the corresponding node and renaming it:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Text::CSV_XS; use XML::LibXML; my $dom = 'XML::LibXML'->load_xml(location => 'test.osm'); my $csv = 'Text::CSV_XS'->new({allow_whitespace => 1, auto_diag => 1, binary => 1}); open my $in, '<:encoding(UTF-8)', 'test.csv' or die $!; $csv->header($in); my $xpath_template = '/osm/node[@lat="%"][@lon="%"]'; while (my $row = $csv->getline($in)) { my $i = 0; my $xpath = $xpath_template =~ s/%/$row->[$i++]/gr; my $node = $dom->findnodes($xpath)->[0]; if ($node) { my $tag = $node->findnodes('tag[@k="name"]')->[0]; $tag->{v} = $row->[2]; } else { warn "@$row missing in XML\n"; } } $dom->toFile('new.osm');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Update XML Values using two primary keys
by pratikpooja (Novice) on Jan 06, 2021 at 15:36 UTC | |
|
Re^2: Update XML Values using two primary keys
by pratikpooja (Novice) on Jan 07, 2021 at 18:57 UTC | |
by hippo (Archbishop) on Jan 07, 2021 at 19:05 UTC | |
by pratikpooja (Novice) on Jan 08, 2021 at 23:09 UTC | |
by hippo (Archbishop) on Jan 09, 2021 at 12:01 UTC | |
by pratikpooja (Novice) on Jan 10, 2021 at 22:34 UTC | |
by hippo (Archbishop) on Jan 11, 2021 at 12:23 UTC | |
by pratikpooja (Novice) on Jan 11, 2021 at 21:20 UTC | |
|