Dear fellow Monks, I have a XHTML file which holds several div tags as shown below. The first group of div's belong to class A and the following to class B and are marked using a class attribute. I would like to parse this part of the file with XML::Simple so that I get an hash with two array references A and B which hold the references to the div content as shown at the end of this post.

I went through the XML::Simple documentation but I didn't find anything suitable. The 'KeyAttr' option does actually create the wanted hash structure but only for one A and one B class div because the class isn't a key attribute here.

Could anyone with more XP with XML::Simple point me out the right option(s). Also maybe an other XML module must be used for that.

Thanks in advance, and sorry if it's really simple and I just oversaw it.

My simple test script so far is:

#!/usr/bin/perl use strict; use warnings; use XML::Simple; my $filename = shift or die "Usage: decode <filename>\n"; my $xmlin; open ($xmlin, '<', $filename) or die "Error: Can't open input file!\n" +; my $ref = XMLin( $xmlin, # TODO: Add correct option ); use Data::Dumper; print Dumper $ref; __END__

The XHTML content:

<div id="main"> <div class="A"> ... </div> <div class="A"> ... </div> <div class="A"> ... </div> <div class="B"> ... </div> <div class="B"> ... </div> <div class="B"> ... </div> </div>

The hash structure I want:

$VAR1 = { 'div' => { 'A' => [ { .. }, { .. }, { .. }, ], 'B' => [ { .. }, { .. }, { .. }, ] } };

In reply to XML::Simple - Make arrays out of class attribute by mscharrer

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.