in reply to Re^9: Update XML Values using two primary keys
in thread Update XML Values using two primary keys

Thank you. Actually, my Hash Key is a combination of lat lon value like 53.58464144.8.560693391. I was trying to separate the lat lon value so that I can add new tags to my XML file with the lat and on value respectively.

I did the split part of lat and lon by using the split and join module.
my @lat_lon_key = (split /[.]/, $key); my $lat_key = join('.', @lat_lon_key[0,1]); my $lon_key = join('.', @lat_lon_key[2,3]);
$VAR1 = { '53.58964145.8.560993391' => { 'name' => 'A99_984_KEI', 'id' => '-1643796' }, '53.58464146.8.560693392' => { 'id' => -1643795, 'name' => 'A88_984_KEI' } };

In the above Hash, if the 1st Hash key is present in the XML then it gets updated but if the 2nd Hash key is not present or rejected then it should get added as new tag in XML.

Now my problem is I am not getting the desired rejected HoH keys in my else part of the statement. What I have to do in else part to get the rejected HoH key and build a new node in XML with it?

Replies are listed 'Best First'.
Re^11: Update XML Values using two primary keys
by hippo (Archbishop) on Jan 13, 2021 at 11:24 UTC
    but if the 2nd Hash key is not present or rejected then it should get added as new tag in XML.

    You can only tell if it is not present after you have trawled through all of the XML nodes, so the logic is this:

    1. Loop over each XML node, if the HoH has the key then use it and then delete it from the HoH
    2. After looping over all the nodes, all the remaining HoH keys are extras to be added, so loop over those keys (sorted if you like) and add them to the XML
    #!/usr/bin/env perl use strict; use warnings; my @nodes = qw/foo quux baz/; my %HoH = ( foo => { day => 'Mon' }, bar => { day => 'Tue' }, baz => { day => 'Wed' } ); # Loop over nodes for my $node (@nodes) { if (exists $HoH{$node}) { print "Node $node found in HoH and deleted\n"; # Do more processing here delete $HoH{$node}; } else { print "Node $node not found in HoH\n"; } } # Loop over remaining unmatched keys for my $key (keys %HoH) { print "Key $key with payload $HoH{$key}->{day} not matched, so add +ed\n"; }

    🦛

      Thank you for your inputs. I am now able to add node id, lat,long values in the XML file but I cannot create a new Child tag inside the new node tag to store the tag name and value.

      Below is my input XML(osm file)
      <?xml version='1.0' encoding='UTF-8'?> <osm version='0.6' upload='false' generator='JOSM'> <bounds minlat='53.560255' minlon='8.5117942' maxlat='53.5974042' ma +xlon='8.6024315' origin='CGImap 0.6.1 (11651 thorn-03.openstreetmap.o +rg)' /> <node id='-1643794' lat='53.58464144' lon='8.560693391'> <tag k='name' v='ASC_A_KEI_001' /> </node> </osm>
      My Output XML File is
      <?xml version="1.0" encoding="UTF-8"?> <osm version="0.6" upload="false" generator="JOSM"> <bounds minlat="53.560255" minlon="8.5117942" maxlat="53.5974042" ma +xlon="8.6024315" origin="CGImap 0.6.1 (11651 thorn-03.openstreetmap.o +rg)"/> <node id="-1643795" lat="53.58464144" lon="8.560693391"> <tag k="name" v="ASC_A_KEI_008"/> </node> <node id="-1643796" lat="60.584" lon="10.560"/><node id="-1643795" lat +="59.584" lon="9.560"/> </osm>
      Here is my updated code
      my $dom = 'XML::LibXML'->load_xml(location => 'test.osm'); for my $node ($dom->findnodes('/osm/node')) { my $tag = $node->findnodes('tag[@k="name"]')->[0]; my $key = "$node->{lat}.$node->{lon}"; if (exists $HoH{$key}){ $tag->{v} = $HoH{$key}->{name}; $node->{id} = $HoH{$key}->{id}; delete $HoH{$key}; } } for my $key (keys %HoH){ for my $node ($dom->findnodes('/osm')){ my @lat_lon_key = (split /[.]/, $key); my $lat_key = join('.', @lat_lon_key[0,1]); my $lon_key = join('.', @lat_lon_key[2,3]); my $elm = $dom->createElement('node'); $elm->setAttribute('id', $HoH{$key}->{id}); $elm->setAttribute('lat', $lat_key); $elm->setAttribute('lon', $lon_key); $node->addChild($elm); } } $dom->toFile('new_test.osm');

      I have been trying to add child tags to my new node tags but I am getting error "XML::LibXML::Node::addChild() -- nNode is not a blessed SV reference".

      I have tried installing the library as well but still getting error. How can I add child tags as well as align my new tags as per input? You can see that the new tags are added side by side and there is no line break. Thank you.

        I got the solution to add tags and align. Below is the updated code to add tags and align.

        my $dom = 'XML::LibXML'->load_xml(location => 'test.osm'); for my $node ($dom->findnodes('/osm/node')) { my $tag = $node->findnodes('tag[@k="name"]')->[0]; my $key = "$node->{lat}.$node->{lon}"; if (exists $HoH{$key}){ $tag->{v} = $HoH{$key}->{name}; $node->{id} = $HoH{$key}->{id}; delete $HoH{$key}; } } for my $key (keys %HoH){ for my $node ($dom->findnodes('/osm')){ my @lat_lon_key = (split /[.]/, $key); my $lat_key = join('.', @lat_lon_key[0,1]); my $lon_key = join('.', @lat_lon_key[2,3]); my $elm = $dom->createElement('node'); $elm->setAttribute('id', $HoH{$key}->{id}); $elm->setAttribute('lat', $lat_key); $elm->setAttribute('lon', $lon_key); my $tag = XML::LibXML::Element->new("tag"); $tag->setAttribute('k','name'); $tag->setAttribute('v',$HoH{$key}->{name}); $elm->addChild($tag); $node->addChild($elm); } } my $pp = XML::LibXML::PrettyPrint->new(indent_string => " "); $pp->pretty_print($dom); $dom->toFile('new_test.osm');

        Thank you very for your help and inputs.