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

Hey All,

I am trying to design some scripts using the module - XML::Parser

To start learning I have a very basic scenario. Suppose I have following XML file:

<root> <tag1>My Tag1</tag1> <tag2>My Tag2</tag2> <tag3>My Tag3</tag3> </root>

I want to save the the tags as the keys of a Hash and respective content as the value of that hash

So for the above XML file using the module XML::Parser, I would like to create a hash having following key/value pair:

my %my_hash = ( tag1 => 'My Tag1', tag2 => 'My Tag2', tag3 => 'My Tag3', );

Is that possible?

Replies are listed 'Best First'.
Re: How to use XML::Parser
by graff (Chancellor) on May 11, 2010 at 06:29 UTC
    For sure, getting your head around the concept of xml parsing can be tough at first, but I've found that the docs and API for XML::Parser are the easiest to work with, in terms of starting with the basics and being able to do just about anything. I posted a snippet a while back that is rather dense but possibly instructive: Get a structured tally of XML tags.

    For what you want, the following would be a place to start:

    #!/usr/bin/perl use strict; use warnings; use XML::Parser; use Data::Dumper 'Dumper'; my $xml_data; { local $/; $xml_data = <DATA>; } my %my_hash; my $hash_key = ''; my $parser = XML::Parser->new( Handlers => { Start => sub { $hash_key = $_[1] }, Char => sub { $my_hash{$hash_key} .= $_[1] if ( $ha +sh_key ) }, End => sub { $hash_key = '' } }, ); $parser->parse( $xml_data ); print Dumper( \%my_hash ); __DATA__ <root> <tag1>My Tag1</tag1> <tag2>My Tag2</tag2> <tag3>My Tag3</tag3> </root>
    (Updated code, adding an "End" handler, and an "if" condition in the "Char" handler, to make sure that "Char" strings from outside the tag(s) of interest do not get appended to the wrong hash values. It still won't do the right thing for more complicated markup structures, like:
    <root> some text <tag1>tag1 text</tag1> some more text <tag2>tag2 text <tag3>tag3 text</tag3> more tag2 text </tag2> and so on... </root>
    But all you need in that case is to manage your "hash_key" tag names as a stack instead of a single value.)
Re: How to use XML::Parser
by Jenda (Abbot) on May 11, 2010 at 10:32 UTC

    Don't use it directly unless you really must.

    use XML::Rules; my $parser = XML::Rules->new(stripspaces => 7, rules => { _default => 'content', root => 'pass' } ); my $my_hash = $parser->parse(\*DATA); use Data::Dumper; print Dumper($my_hash); __DATA__ <root> <tag1>My Tag1</tag1> <tag2>My Tag2</tag2> <tag3>My Tag3</tag3> </root>

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

Re: How to use XML::Parser
by ambrus (Abbot) on May 11, 2010 at 08:35 UTC
Re: How to use XML::Parser
by Anonymous Monk on May 11, 2010 at 06:17 UTC
    Is that possible?

    Yes.

Re: How to use XML::Parser
by ikegami (Patriarch) on May 19, 2010 at 23:18 UTC
    You appear to be reinventing XML::Simple.
    use strict; use warnings; use Data::Dumper qw( Dumper ); use XML::Simple qw( :strict ); my $xml = do { local $/; <DATA> }; my $tree = do { local $XML::Simple::PREFERRED_PARSER = 'XML::Parser'; XMLin($xml, ForceArray => [], KeyAttr => {}, ) }; print(Dumper($tree)); __DATA__ <root> <tag1>My Tag1</tag1> <tag2>My Tag2</tag2> <tag3>My Tag3</tag3> </root>
Re: How to use XML::Parser
by paragkalra (Scribe) on May 11, 2010 at 06:28 UTC

    How?

    Documentation doesn't have sufficient examples for the beginners. :(

Re: How to use XML::Parser
by paragkalra (Scribe) on May 19, 2010 at 22:44 UTC

    Thanks Graf, your algo worked like magic. But there is one issue. Some of the tags may have a null value.

    In that case, hash key is not getting assigned.

    EG:
    <root> <tag1></tag1> <tag2>My Tag2</tag2> <tag3>My Tag3</tag3> </root>

    So for the above case, hash data structure should be:

    my %my_hash = ( tag1 => undef, tag2 => 'My Tag2', tag3 => 'My Tag3', );