in reply to Accessing attributes in XML::Simple

To use
foreach $e (@{$data->{footnotes}}) { print "$e->{footnote}{content}\n"; }

You'd need to specify KeyAttr => [].

With the default KeyAttr => [qw( name key id )], the above should be

my $footnotes = $data->{footnotes}{footnote}; foreach my $id (keys %$footnotes) { print "$footnotes->{$id}{content}\n"; }
or
my $footnotes = $data->{footnotes}{footnote}; foreach my $footnote (values %$footnotes) { print "$footnote->{content}\n"; }

Don't forget ForceArray => [qw( car footnote )]. XML::Simple is the hardest XML parser to use :(

By the way, if you used GroupTags => { footnotes => 'footnote' }, you could write ->{footnotes}{footnote} as just ->{footnotes}

Replies are listed 'Best First'.
Re^2: Accessing attributes in XML::Simple
by Jenda (Abbot) on Jan 02, 2010 at 12:48 UTC

    Considering the size of XML::Simple's docs (including all those options people tend to babble about as making XML::Simple complex) is about one tenth of the size of say XML::LibXML's (even if we ignore SAX and the fact that you have to learn XPath from some other document), I find your statement fairly ... interesting.

    XML::Simple is so simple that people tend to forget they have to set the options right. That's its only problem. Other than that for a reasonably designed data-centric XML of reasonable size, it's the easiest. If for no reason than because it doesn't force you to learn yet another set of method names to navigate what's basically just a tree data structure.

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

Re^2: Accessing attributes in XML::Simple
by sherab (Scribe) on Jan 05, 2010 at 03:28 UTC
    Your example works GREAT for me ikegami. What I'm puzzled by now is how to refer to values of "F1", "F2", etc of the XML. The "id=" portion of the iteration. My apologies but I have been doing this for so long my brain is tapioca.

      In the first snippet, $e->{footnote}{id}

      In the second snippet, $id

      In the third snippet, you can't.

      Oh Jeeez,... I should probably stop for the night. The answer is "$id". Can't believe I asked the question. Sorry folks!