当前位置 : 主页 > 网页制作 > xml >

xml – 如何使用perl解析KML文件?

来源:互联网 收集:自由互联 发布时间:2021-06-13
我试图使用perl解析KML文件.我正在尝试使用 XML :: Simple模块来执行此操作. 我想通过每个地标来提取一些数据 1)Mcode 2)坐标 阅读了几篇文章之后,我尝试了以下方法来打印所有点的坐标,但
我试图使用perl解析KML文件.我正在尝试使用 XML :: Simple模块来执行此操作.

我想通过每个地标来提取一些数据

1)Mcode
2)坐标

阅读了几篇文章之后,我尝试了以下方法来打印所有点的坐标,但它失败了

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

my $myFile = XMLin('ExperimentMap.kml');

foreach my $folder (@{$myFile->{Document}->{Folder}->{Placemark}})  {
    print $folder->{Point}->{coordinates}."\n";
}

我使用以下KML:

<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
    <Document>
        <name>ExperimentMap</name>
        <description><![CDATA[]]></description>
        <Folder>
            <name>ExperimentLayer</name>
            <Placemark>
                <styleUrl>#icon-503-FF8277</styleUrl>
                <name>home</name>
                <ExtendedData>
                    <Data name='string'>
                        <displayName>Mcode</displayName>
                        <value>PLAAB</value>
                    </Data>
                </ExtendedData>
                <ExtendedData>
                    <Data name='string'>
                        <displayName>postal code</displayName>
                        <value>450010</value>
                    </Data>
                </ExtendedData>
                <description><![CDATA[my home


Mcode: PLAAB
postal code: 450010]]></description>
                <Point>
                    <coordinates>80.23435592651367,13.094024942328286,0.0</coordinates>
                </Point>
            </Placemark>
            <Placemark>
                <styleUrl>#icon-503-FF8277</styleUrl>
                <name>shop</name>
                <ExtendedData>
                    <Data name='string'>
                        <displayName>Mcode</displayName>
                        <value>XMPLE</value>
                    </Data>
                </ExtendedData>
                <ExtendedData>
                    <Data name='string'>
                        <displayName>postal code</displayName>
                        <value>450013</value>
                    </Data>
                </ExtendedData>
                <description><![CDATA[my shop


Mcode: XMPLE
postal code: 450013]]></description>
                <Point>
                    <coordinates>80.2437973022461,13.106230102044576,0.0</coordinates>
                </Point>
            </Placemark>
            <Placemark>
                <styleUrl>#icon-503-FF8277</styleUrl>
                <name>place3</name>
                <ExtendedData>
                    <Data name='string'>
                        <displayName>Mcode</displayName>
                        <value>UDKBL</value>
                    </Data>
                </ExtendedData>
                <ExtendedData>
                    <Data name='string'>
                        <displayName>postal code</displayName>
                        <value>578635</value>
                    </Data>
                </ExtendedData>
                <description><![CDATA[my school


Mcode: UDKBL
postal code: 578635]]></description>
                <Point>
                    <coordinates>80.24688720703125,13.096198508196448,0.0</coordinates>
                </Point>
            </Placemark>
        </Folder>
        <Style id='icon-503-FF8277'>
            <IconStyle>
                <color>ff7782FF</color>
                <scale>1.1</scale>
                <Icon>
                    <href>http://www.gstatic.com/mapspro/images/stock/503-wht-blank_maps.png</href>
                </Icon>
            </IconStyle>
        </Style>
    </Document>
</kml>
怎么样:

while (my ($key, $folder) = each %{$myFile->{Document}{Folder}{Placemark}})  {
    print $folder->{Point}->{coordinates}."\n";
}

输出:

80.2437973022461,13.106230102044576,0.0
80.24688720703125,13.096198508196448,0.0
80.23435592651367,13.094024942328286,0.0

在您的代码中,您正在使用

foreach my $folder (@{$myFile->{Document}->{Folder}->{Placemark}})  {
    print $folder->{Point}->{coordinates}."\n";
}

假设$myFile-> {Document} – > {Folder} – > {Placemark}是对数组的引用.但事实并非如此,它是对散列的引用,因此您必须遍历它并且foreach对(key,value)从值中提取坐标.

网友评论