A URI can't be a HASH reference at /System/Library/Perl/Extras/5.8.8/LWP/Simple.pm line 113

Is there some way that I can ignore <icon-link xsi:nil="true"/>

The error message points you to a possible way around the problem: test if ->[0] is a hashref, and if so, skip to the next entry, or skip that case entirely, or whatever you'd consider an appropriate workaround in case of missing data.

The thing is that XML::Simple is creating an extra hashref as soon as the XML tag has an attribute (such as <icon-link foo="bar" ...).  You can always use Data::Dumper to figure out such things yourself. Consider the following:

#!/usr/bin/perl use XML::Simple; use Data::Dumper; my $xml_parser = XML::Simple->new(); for my $icon_link_xml ( '<icon-link>http://www.nws.noaa.gov/weather/images/fcicons/sct.jpg +</icon-link>', '<icon-link xsi:nil="true"/>', '<icon-link foo="bar"/>', '<icon-link foo="bar">http://www.nws.noaa.gov/weather/images/fcico +ns/sct.jpg</icon-link>', ) { my $xml = <<"EOXML"; <?xml version='1.0' ?> <dwml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <data> <parameters> <conditions-icon> <name>Conditions Icons</name> $icon_link_xml <icon-link>http://www.nws.noaa.gov/weather/images/fcicons/nra3 +0.jpg</icon-link> </conditions-icon> </parameters> </data> </dwml> EOXML # print "$xml\n"; my $data = $xml_parser->XMLin($xml); # print Dumper $data; my $icon_link = $data->{'data'}{'parameters'}{'conditions-icon'}{' +icon-link'}; print Dumper $icon_link; # use the next entry, for example my $idx = ref($icon_link->[0]) eq "HASH" ? 1 : 0; my $weatherIconURL = $icon_link->[$idx]; # or skip entirely #next if ref($icon_link->[0]) eq "HASH"; print "=> \$weatherIconURL: $weatherIconURL\n\n"; } __END__ $VAR1 = [ 'http://www.nws.noaa.gov/weather/images/fcicons/sct.jpg', 'http://www.nws.noaa.gov/weather/images/fcicons/nra30.jpg' ]; => $weatherIconURL: http://www.nws.noaa.gov/weather/images/fcicons/sct +.jpg $VAR1 = [ { 'xsi:nil' => 'true' }, 'http://www.nws.noaa.gov/weather/images/fcicons/nra30.jpg' ]; => $weatherIconURL: http://www.nws.noaa.gov/weather/images/fcicons/nra +30.jpg $VAR1 = [ { 'foo' => 'bar' }, 'http://www.nws.noaa.gov/weather/images/fcicons/nra30.jpg' ]; => $weatherIconURL: http://www.nws.noaa.gov/weather/images/fcicons/nra +30.jpg $VAR1 = [ { 'content' => 'http://www.nws.noaa.gov/weather/images/fcico +ns/sct.jpg', 'foo' => 'bar' }, 'http://www.nws.noaa.gov/weather/images/fcicons/nra30.jpg' ]; => $weatherIconURL: http://www.nws.noaa.gov/weather/images/fcicons/nra +30.jpg

Alternatively, you could set ForceContent => 1, and then use

$weatherIconURL = $data->{'data'}{'parameters'}{'conditions-icon'} +{'icon-link'}[0]{content};

In that case, the URL would simply be undefined (for <icon-link xsi:nil="true"/>) — which you'd have to check for as well, of course, to prevent further steps from erroring out when trying to fetch it...


In reply to Re: Problem Parsing XML with Perl by almut
in thread Problem Parsing XML with Perl by Perobl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.