如何在不使用Data :: Dumper打印输出(解析数据)的情况下解析kml文件

时间:2016-06-20 15:56:27

标签: perl kml

我是perl的新手,我使用perl来解析kml文件。我正在使用的代码有效。但是,我目前正在使用data :: dumper打印解析到的文件,这使得它很麻烦,因为我必须稍后读取该文件。我想知道是否有更好的解决方案? 这是我的代码:

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});

这是样本数据:

    <?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>

1 个答案:

答案 0 :(得分:1)

更好的解决方案是 - 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"

是解决问题的最佳猜测,代替实际数据和示例输出。

对于一些数据,我认为这样的事情会做到这一点。你的XML已被破坏,所以我不能100%确定(我可能会稍后修复它)。

#!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"; 
}