如何在不使用 Data::Dumper 打印输出(已解析数据)的情况下解析 kml 文件
How to parse a kml file without using Data::Dumper to print output(parsed data)
我是 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>
更好的解决方案是 -
#!/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";
}
我是 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>
更好的解决方案是 -
#!/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";
}