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.


In reply to Re^12: Update XML Values using two primary keys by pratikpooja
in thread Update XML Values using two primary keys by pratikpooja

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.