pratikpooja has asked for the wisdom of the Perl Monks concerning the following question:
Hello Members, I am working on a perl program and I am new to it. I have to update XML (i.e. osm file) by taking values from CSV file. I have a csv file with values as
My XML file(i.e osm file) is"latitude"; "longitude"; "name" 53.58464144;8.560693391;"AK2_PKW_A_001" 53.5777080210388;8.56104893106605;"AK2_PKW_A_002"
I have to update the name tag in XML file with the name coming from csv file by comparing the lat. and long. value from csv file with the lat. and long. value of XML file. I have written a code to get the values from XML. Can you please help me how to proceed further?. Thank you very much.<?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> <way id='-1653459' action='modify'> <nd ref='-1652714' /> <nd ref='-1652766' /> <nd ref='-1652768' /> <tag k='highway' v='service' /> <tag k='lanes:backward' v='1' /> <tag k='lanes:forward' v='1' /> <tag k='name' v='FW_W1_52' /> <tag k='vehicle' v='private' /> </way> </osm>
#!/usr/bin/perl use strict; use warnings 'all'; use feature 'say'; use Getopt::Long; use Getopt::Std; use Getopt::Long qw(GetOptions); use XML::LibXML; use Data::Dumper; my $filenameMap = 'test.osm'; my($dom) = XML::LibXML->load_xml(location => $filenameMap); my $doc = $dom->documentElement; my @children = $doc->childNodes; my $countChildren = @children; my @toUpdateNodes = (); foreach my $child (@children) { my @childFromNodes = $child->childNodes; my $countChildFromNodes = @childFromNodes; foreach my $childNode (@childFromNodes) { #print $childNode->nodeType, "\n" ; if($childNode->nodeType == XML_ELEMENT_NODE && $childNode->has +Attribute("k")) { my $nodeVal = $childNode->getAttribute("v"); print "NodeValue: ", $childNode->getAttribute("v"), "\n"; } } }
|
|---|