0

I am fairly new to perl and i am using perl to parse a kml file. The code I am using works. However, i am currently using data::dumper to print the parsed to a file, which makes it troublesome since i have to read the file later. I was wondering if there was a better solution? Here is my code:

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

open (OUTPUT1,">output1.txt");
my $xml = new XML::Simple;
my $data = $xml->XMLin("test1.kml");

print OUTPUT1 Dumper($data->{Document}->{Folder}->{Folder});

here is the sample data:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
  <name>name</name>
  <open>1</open>
  <Style id="redStar" xmlns="">
  <IconStyle>
    <color>FFFFFF00</color>
    <scale>0.7</scale>
    <Icon>
      <href>http://maps.google.com/mapfiles/kml/shapes/donut.png</href>
    </Icon>
  </IconStyle>
  <LabelStyle>
    <scale>1.5</scale>
  </LabelStyle>
</Style>
<Style id="yellowCircle" xmlns="">\
    <IconStyle><color>FF66FF00</color><scale>0.7</scale><Icon>     <href>http://maps.google.com/mapfiles/kml/shapes/square.png</href></Icon>    </IconStyle><LabelStyle><scale>0.8</scale></LabelStyle></Style>
<Style id="whiteCircle" xmlns="">
  <IconStyle>
    <color>FF0000AA</color>
    <scale>0.7</scale>
    <Icon>
      <href>http://maps.google.com/mapfiles/kml/paddle/wht-circle.png</href>
    </Icon>
  </IconStyle>
  <LabelStyle>
    <scale>0.8</scale>
  </LabelStyle>
</Style>
<Style id="s_ylw-pushpin" xmlns="">
  <IconStyle>
    <scale>0.7</scale>
    <Icon>
      <href>http://maps.google.com/mapfiles/kml/paddle/grn-diamond.png</href>
    </Icon>
    <hotSpot x="32" y="1" xunits="pixels" yunits="pixels" />
  </IconStyle>
  <ListStyle>
    <ItemIcon>
      <href>http://maps.google.com/mapfiles/kml/paddle/grn-diamond-lv.png</href>
    </ItemIcon>
  </ListStyle>
</Style>
<Style id="s_ylw-pushpin_hl" xmlns="">
  <IconStyle>
    <scale>0.827273</scale>
    <Icon>
      <href>http://maps.google.com/mapfiles/kml/paddle/grn-diamond.png</href>
    </Icon>
    <hotSpot x="32" y="1" xunits="pixels" yunits="pixels" />
  </IconStyle>
  <ListStyle>
    <ItemIcon>
      <href>http://maps.google.com/mapfiles/kml/paddle/grn-diamond-    lv.png</href>
    </ItemIcon>
  </ListStyle>
</Style>
<Style id="blueCircle" xmlns="">
  <IconStyle>
    <color>FF0000FF</color>
    <scale>1.0</scale>
    <Icon>
                    <href>http://maps.google.com/mapfiles/kml/shapes/placemark_square.png</href>
    </Icon>
  </IconStyle>
  <LabelStyle>
    <scale>0.8</scale>
  </LabelStyle>
</Style>
<Style id="greenCircle" xmlns="">
  <IconStyle>
    <scale>0.7</scale>
    <Icon>
      <href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href>
    </Icon>
  </IconStyle>
  <LabelStyle>
    <scale>0.8</scale>
  </LabelStyle>
</Style>
<Folder>
<name>Circuits</name>
  <Folder>
    <name>Circuit</name>
    <Folder>
      <name>Segment</name>
      <Placemark>
        <name>Segment1</name>
        <Style>
          <LineStyle id="#20605123_ARFHC.Style">
            <color>FF00FF00</color>
            <width>2</width>
          </LineStyle>
        </Style>
        <LineString>
          <altitudeMode>clampToGround</altitudeMode>
          <coordinates>000.00000,0.000000
 </coordinates>
        </LineString>
      </Placemark>
 <Folder>
      <name>Manhole</name>
      <Placemark>
        <name>Manhole1</name>
        <styleUrl>#blueCircle</styleUrl>
        <Point>
          <coordinates>111.11111,111.111111
</coordinates>
            </Point>
          </Placemark>
          <Placemark>
             <name>Manhole2</name>
            <styleUrl>#blueCircle</styleUrl>
            <Point>
              <coordinates>111.444444,3.2222222
</coordinates>
            </Point>
          </Placemark>
  </Folder>
      </Folder>
    </Folder>
  </Document>
</kml>
jpjy
  • 25
  • 5
  • 1
    Sample data would help massively giving you an answer. – Sobrique Jun 20 '16 at 16:00
  • How is the printing and Data::Dumper part related to parsing another file? – simbabque Jun 20 '16 at 16:03
  • 1
    Your code doesn't read any file constantly. What exactly is your problem? – choroba Jun 20 '16 at 16:03
  • What is a KML file? I'm thinking of the files that Google Earth uses, but they don't have Document and Folder elements. Data::Dumper isn't doing any parsing here, that's XML::Simple. I'm afraid you haven't done a very good job of explaining what you want to do. – Dave Cross Jun 20 '16 at 16:09
  • i apologise. Basically, when i was learning how to parse the file, most of the sample codes i saw used data::dumper to print out the data. However, in my case, i will need to read the file later(this is the first part of my project ), and i read that this is inadvisable for data::dumper. Therefore, i was wondering if there was a better way to print out the parsed data. – jpjy Jun 20 '16 at 16:16
  • Show us (a sample of) the data you're parsing. Almost certainly is the answer. – Sobrique Jun 20 '16 at 16:21
  • Added sample data. Basically im interested in getting data of the manholes. My script is able to get it, but i read that data::dumper is a bad choice if you need to read the script again, which i will need to do at a later part. therefore i was wondering if there are any better ways to print the data – jpjy Jun 20 '16 at 16:41
  • OK. And what's the desired output? – Sobrique Jun 20 '16 at 16:56
  • 1
    "Manhole1" => { Point => { coordinates => "111.11111,111.111111" }, styleUrl => "#blueCircle", }, "Manhole2" => { Point => { coordinates => "111.444444,3.2222222" }, styleUrl => "#blueCircle", }, -- my current output is something like this, which is fine. Though my desired output would be some like : Manhole1 : coordinates: xxxxx – jpjy Jun 20 '16 at 16:58

1 Answers1

1

A better solution would be - don't use XML::Simple

#!/usr/bin/env perl
use strict;
use warnings; 

use XML::Twig; 

open ( my $output, '>', 'output1.txt' ) or die $!;
print {$output} XML::Twig -> parsefile ( 'test1.kml' ) -> get_xpath('//Folder/Folder',0)->text, "\n"

Is a best guess at solving your problem, in lieu of actual data, and example output.

With some data, something like this will do it - I think. Your XML is broken, so I'm not 100% sure (I may have a go at fixing it later).

#!usr/bin/perl
use strict;
use warnings;

use XML::Twig;

my $twig = XML::Twig -> parsefile('input.kml'); 

foreach my $placemark ( $twig -> get_xpath('//PlaceMark') ) {
    next unless $placemark -> first_child_text('name') =~ m/Manhole/;  
    print join ( ",", ( map { $placemark -> get_xpath($_) -> text } qw ( name .//coordinates .//styleUrl ))),"\n"; 
}
Community
  • 1
  • 1
Sobrique
  • 52,974
  • 7
  • 60
  • 101