asa has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to parse a XML file, but I get this error Not a HASH reference at ./parser.pl line 42

At Line 42,

print REPORT '<td>' . $e->{desc}->{descript}->{content} . '</td><td><br>';
When I checked the line that cause this issue in the XML file, I can see maybe because the tag is repeated. Not really sure!
<entry type="CVE" severity="Medium" seq="1999-0453" published="1999-01 +-01" name="CVE-1999-0453" modified="2008-09-05" CVSS_version="2.0" CV +SS_vector="(AV:N/AC:L/Au:N/C:P/I:N/A:N)" CVSS_score="5.0" CVSS_impact +_subscore="2.9" CVSS_exploit_subscore="10.0" CVSS_base_score="5.0"> <desc> <descript source="cve">An attacker can identify a CISCO device b +y sending a SYN packet to port 1999, which is for the Cisco Discovery + Protocol (CDP).</descript> <descript source="nvd">Please see the following link for more in +formation:http://seclists.org/bugtraq/1999/Jan/0215.html</descript> </desc>
I think I should add a keyattr, but I don't know how! I think it should be something like this KeyAttr    => {descript=>"+source"} I have already this part in the code
$data = $xml->XMLin( $ARGV[0], KeyAttr => { cve => 'entry'}, ForceArray => [ 'cve', 'ref' ] );
Thanks.

Replies are listed 'Best First'.
Re: XML parsing
by tobyink (Canon) on Jan 09, 2012 at 08:29 UTC

    This is why XML::Simple is anything but simple. Given:

    use XML::Simple; use Data::Dumper; print Dumper XMLin do { local $/ = <DATA> }; __DATA__ <log> <entry> <date>2012-01-08</date> <text>Test</text> </entry> <entry> <date>2012-01-09</date> <text>Test</text> <text>Additional information</text> </entry> </log>

    ... as you can see, for each entry, the value for the "text" key is sometimes a string and sometimes an arrayref. You need to either test the value of "text" with ref to see if it's an arrayref, or pass additional options to XMLin to force it to always be an array. Once you've got a few potentially duplicable elements, this really starts to eliminate the supposed simplicity of XML::Simple.

    Better to start straight out with something like XML::LibXML, which might present a more complicated API to begin with, it's at least consistent. Even if your XML gets more complex, your script doesn't have to.

      Thanks I will check it, but still want to know what is the wrong in this simple code. This confuses me.

      Thanks.

        The wrong thing is that if the tag is repeated, you end up with an array reference, if it's not, you end up with a string (if it contains just content and no attributes or child tags) or hash reference (if it does have attributes or children). Do see Simpler than XML::Simple.

        The code in the root node would throw a "Not a hash reference" exception if there are more <descript> tags in one desc, the node in your other node would throw "Not an array reference" if there is just one.

        You are trying to use the ForceArray option, but you failed to include "descript" in the list. That would force XML::Simple to always produce an array reference so that the second code works.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: XML parsing
by trizen (Hermit) on Jan 09, 2012 at 01:35 UTC
    Try this:
    use 5.010; use XML::Simple; use Data::Dumper; my $ref = XMLin(join('', <DATA>)); print Dumper($ref); foreach my $i (0 .. $#{$ref->{'desc'}{'descript'}}) { say $ref->{'desc'}{'descript'}[$i]{'content'}; } __DATA__ <entry type="CVE" severity="Medium"> <desc> <descript source="cve">test1</descript> <descript source="nvd">test2</descript> </desc> </entry>

      Actually I was trying something like this

      foreach $desc ( @{ $e->{desc}->{descript} } ) { print REPORT $desc->{source}.':'; print REPORT $desc->{content}. '<br>'; }
      But I don't understand why it doesn't work! Not an ARRAY reference at ./parser.pl line 47. Thanks.