我在访问从perl Data :: Dumper返回的元素/数组时遇到问题。我可以访问以下结果:
$VAR1 = {
'head' => {
'source' => {
'credit-logo' => 'foo',
'feedback' => 'bar',
'more-information' => 'google',
通过以下行,我可以获得'credit-logo'值:
$credit= $result->{head}->{'source'}->{'credit-logo'};
但是,我遇到以下问题,整个事情以“[”而不是“{”开头:
$VAR1 = [
'dwml',
{
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'version' => '1.0',
'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
'xsi:noNamespaceSchemaLocation' => 'site'
},
[
[
'head',
{},
[
[
'product',
{
'concise-name' => 'time-series',
'srsName' => 'WGS 1984',
'operational-mode' => 'official'
在上面,我将如何访问'srsName'值?
无论我尝试什么,我都会得到关于价值不是HASH或ARRAY的消息。
- 我的原始脚本正试图从NWS获取预测数据:
#!/usr/bin/perl
use NDFD::Weather::Report;
use Data::Dumper;
$result=NDFD::Weather::Processer->new(
listLatLon=>'30.3369,-81.6614',
product=>'time-series',
Unit=>'m',
maxt =>0,
mint =>0,
temp =>1,
dew =>1,
pop12 =>0,
qpf =>0,
sky =>1,
snow =>0,
wspd =>1,
wdir =>1,
wx =>1,
waveh =>0,
icons =>0,
rh =>0,
appt =>0,
incw34 =>0,
incw50 =>0,
incw64 =>0,
cumw34 =>0,
cumw50 =>0,
cumw64 =>0,
conhazo =>0,
ptornado =>0,
phail =>0,
ptstmwinds =>0,
pxtornado =>0,
pxhail =>0,
pxtstmwinds =>0,
ptotsvrtstm =>0,
pxtotsvrtstm =>0,
tmpabv14d =>0,
tmpblw14d =>0,
tmpabv30d =>0,
tmpblw30d =>0,
tmpabv90d =>0,
tmpblw90d =>0,
prcpabv14d =>0,
prcpblw14d =>0,
prcpabv30d =>0,
prcpblw30d =>0,
prcpabv90d =>0,
prcpblw90d =>0,
precipa_r =>0,
sky_r =>0,
td_r =>0,
temp_r =>0,
wdir_r =>0,
wspd_r =>0,
wgust =>0
);
$value= $result->NDFDgenLatLonList;
$xml_handler1=$result->Parse_XML_Data_decode($value);
print Dumper $xml_handler1;
我的目标是提取从该Dumper响应中返回的值。
这是返回的XML,添加:print $ value;
-
<?xml version="1.0"?>
<dwml version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.nws.noaa.gov/forecasts/xml/DWMLgen/schema/DWML.xsd">
<head>
<product srsName="WGS 1984" concise-name="time-series" operational-mode="official">
<title>NOAA's National Weather Service Forecast Data</title>
<field>meteorological</field>
<category>forecast</category>
<creation-date refresh-frequency="PT1H">2015-10-02T15:56:10Z</creation-date>
</product>
<source>
<more-information>http://www.nws.noaa.gov/forecasts/xml/</more-information>
<production-center>Meteorological Development Laboratory<sub-center>Product Generation Branch</sub-center></production-center>
<disclaimer>http://www.nws.noaa.gov/disclaimer.html</disclaimer>
<credit>http://www.weather.gov/</credit>
<credit-logo>http://www.weather.gov/images/xml_logo.gif</credit-logo>
<feedback>http://www.weather.gov/feedback.php</feedback>
</source>
</head>
<data>
<location>
<location-key>point1</location-key>
<point latitude="30.34" longitude="-81.66"/>
</location>
<moreWeatherInformation applicable- >
<time-layout time-coordinate="local" summarization="none">
<layout-key>k-p3h-n35-1</layout-key>
<start-valid-time>2015-10-02T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-02T17:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-02T20:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-02T23:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T02:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T05:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T08:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T11:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T17:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T20:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T23:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T02:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T05:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T08:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T11:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T17:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T20:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-05T02:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-05T08:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-05T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-05T20:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-06T02:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-06T08:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-06T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-06T20:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-07T02:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-07T08:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-07T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-07T20:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-08T02:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-08T08:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-08T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-08T20:00:00-04:00</start-valid-time>
</time-layout>
<time-layout time-coordinate="local" summarization="none">
<layout-key>k-p3h-n19-2</layout-key>
<start-valid-time>2015-10-02T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-02T17:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-02T20:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-02T23:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T02:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T05:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T08:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T11:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T17:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T20:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-03T23:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T02:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T05:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T08:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T11:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T14:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T17:00:00-04:00</start-valid-time>
<start-valid-time>2015-10-04T20:00:00-04:00</start-valid-time>
</time-layout>
<parameters applicable-location="point1">
<temperature type="hourly" units="Celsius" time-layout="k-p3h-n35-1">
<name>Temperature</name>
<value>23</value>
<value>23</value>
<value>21</value>
<value>20</value>
<value>19</value>
<value>18</value>
<value>18</value>
<value>23</value>
<value>26</value>
<value>25</value>
<value>22</value>
<value>21</value>
<value>20</value>
<value>19</value>
<value>19</value>
<value>24</value>
<value>26</value>
<value>26</value>
<value>22</value>
<value>19</value>
<value>18</value>
<value>27</value>
<value>23</value>
<value>21</value>
<value>19</value>
<value>27</value>
<value>23</value>
<value>21</value>
<value>20</value>
<value>27</value>
<value>24</value>
<value>22</value>
<value>21</value>
<value>28</value>
<value>24</value>
</temperature>
<temperature type="dew point" units="Celsius" time-layout="k-p3h-n35-1">
<name>Dew Point Temperature</name>
<value>20</value>
<value>20</value>
<value>19</value>
<value>18</value>
<value>17</value>
<value>17</value>
<value>17</value>
<value>17</value>
<value>18</value>
<value>18</value>
<value>18</value>
<value>17</value>
<value>17</value>
<value>17</value>
<value>17</value>
<value>17</value>
<value>17</value>
<value>18</value>
<value>18</value>
<value>18</value>
<value>18</value>
<value>19</value>
<value>19</value>
<value>18</value>
<value>18</value>
<value>19</value>
<value>18</value>
<value>18</value>
<value>18</value>
<value>19</value>
<value>19</value>
<value>19</value>
<value>19</value>
<value>19</value>
<value>20</value>
</temperature>
<wind-speed type="sustained" units="meters/second" time-layout="k-p3h-n35-1">
<name>Wind Speed</name>
<value>5</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>5</value>
<value>5</value>
<value>5</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>3</value>
<value>2</value>
<value>3</value>
<value>3</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>5</value>
<value>4</value>
<value>3</value>
<value>4</value>
<value>4</value>
<value>4</value>
<value>3</value>
<value>4</value>
<value>5</value>
<value>4</value>
</wind-speed>
<direction type="wind" units="degrees true" time-layout="k-p3h-n35-1">
<name>Wind Direction</name>
<value>0</value>
<value>360</value>
<value>340</value>
<value>340</value>
<value>320</value>
<value>310</value>
<value>300</value>
<value>280</value>
<value>280</value>
<value>270</value>
<value>280</value>
<value>280</value>
<value>270</value>
<value>260</value>
<value>240</value>
<value>250</value>
<value>250</value>
<value>240</value>
<value>240</value>
<value>260</value>
<value>300</value>
<value>350</value>
<value>360</value>
<value>350</value>
<value>350</value>
<value>10</value>
<value>10</value>
<value>20</value>
<value>10</value>
<value>30</value>
<value>40</value>
<value>40</value>
<value>40</value>
<value>60</value>
<value>60</value>
</direction>
<cloud-amount type="total" units="percent" time-layout="k-p3h-n35-1">
<name>Cloud Cover Amount</name>
<value>100</value>
<value>100</value>
<value>100</value>
<value>99</value>
<value>97</value>
<value>90</value>
<value>79</value>
<value>71</value>
<value>64</value>
<value>72</value>
<value>79</value>
<value>78</value>
<value>76</value>
<value>72</value>
<value>69</value>
<value>69</value>
<value>51</value>
<value>51</value>
<value>35</value>
<value>28</value>
<value>64</value>
<value>63</value>
<value>63</value>
<value>62</value>
<value>62</value>
<value>60</value>
<value>48</value>
<value>37</value>
<value>32</value>
<value>35</value>
<value>37</value>
<value>41</value>
<value>45</value>
<value>40</value>
<value>31</value>
</cloud-amount>
<wind-speed type="gust" units="meters/second" time-layout="k-p3h-n19-2">
<name>Wind Speed Gust</name>
<value>7</value>
<value>6</value>
<value>6</value>
<value>6</value>
<value>6</value>
<value>6</value>
<value>6</value>
<value>7</value>
<value>7</value>
<value>7</value>
<value>5</value>
<value>6</value>
<value>6</value>
<value>6</value>
<value>5</value>
<value>5</value>
<value>6</value>
<value>6</value>
<value>4</value>
</wind-speed>
<weather time-layout="k-p3h-n35-1">
<name>Weather Type, Coverage, and Intensity</name>
<weather-conditions>
<value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="slight chance" intensity="none" additive="and" weather-type="thunderstorms" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="moderate" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="slight chance" intensity="none" additive="and" weather-type="thunderstorms" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
<value coverage="slight chance" intensity="none" additive="and" weather-type="thunderstorms" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions>
<value coverage="slight chance" intensity="light" weather-type="rain showers" qualifier="none">
<visibility xsi:nil="true"/>
</value>
</weather-conditions>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
<weather-conditions/>
</weather>
</parameters>
答案 0 :(得分:1)
您似乎正在使用XML::Parser->new(Style => 'Tree')
。这个解析器的界面相当简单,这是一个很好的方式,说它不是非常用户友好。你可以通过计算括号来弄清楚如何做到这一点:
$VAR -> [2][0][2][0][1]{'srsName'};
但严重的是,有更友好的选择。只需使用XML::Twig
或XML::LibXML
,因为以上内容变为:
$twig -> findnodes('//srsName',0)->trimmed_text;
(或类似的东西 - 就像我说的,给我们一些XML,我们可以给你一个确切的答案)。
编辑:
像这样(NB:未经测试,因为我无法下载该模块):
use XML::Twig;
my $value= $result->LatLonListCityNames;
my $twig = XML::Twig -> parse ( $value );
my $srsName_element = $twig -> findnodes ( '//srsName', 0 );
$srsName_element -> print;
print $srsName_element -> trimmed_text;
答案 1 :(得分:0)
您遇到问题的原因是<ul id="sector-nav" class="nav">
<li >
<!--<a href="&Itemid=§or_id=1">Fibre</a>-->
<a href="sectors.php?<?php echo $id=1; ;?>">Fibre</a>
</li>
<li >
<!--<a href="&Itemid=§or_id=2">Hand Protection</a>-->
<a href="sectors.php?<?php echo $id=2; ?>">Hand Protection</a>
</li>
<li >
<!--<a href="&Itemid=§or_id=3">Purification Products</a>-->
<a href="sector_id=3.html">Purification Products</a>
</li>
<!-- others <li>'s -->
</ul>
表示数据包含在数组引用中,而不是哈希引用中,因此必须以不同方式访问它。
让我们尝试将其分解(用占位符替换所有内容)
[
只需关注$VAR1 = [
0 (string),
1 (hash ),
--> 2 (array ) [
--> 0 (array) [
0 (string),
1 (hash ),
--> 2 (array ) [
--> 0 (array) [
0 (string )
--> 1 (hash ) {
--> 'srsName' => $val
即可。每个指向您必须访问的下一个元素来遍历数据结构。
我没有对此进行测试,但似乎可以通过
访问它 -->
正如许多其他人所说,使用my $srsName = $xml_handler1->[2][0][2][0][1]{'srsName'};
并手动访问数据显然不是解析XML的好方法。我个人不熟悉&#34;适当的&#34;你应该用这些方法,但我建议你阅读评论并看看Sobrique的答案。
旁注...您应始终将Data::Dumper
和use strict
添加到脚本的顶部,并使用use warnings
以词汇方式定义所有变量。它可以防止不良的编码习惯,并且可以为您节省很多麻烦,试图调试代码并且很难找到问题。
有关原因的列表,请查看此article。