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

Monks, Here is my xml file
<booklist> <book type="technical"> <author>Book 1 author 1</author> <author>Book 1 author 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> </book> <book type="fiction"> <author>Book 2 author 1</author> <author>Book 2 author 2</author> <title>Book 2 title</title> <isbn>Book2ISBN</isbn> </book> <book type="technical"> <author>Book 3 author 1</author> <author>Book 3 author 2</author> <author>Book 3 author 3</author> <title>Book 3 title</title> <isbn>Book3ISBN</isbn> </book> </booklist>
Here is my perl code to parse it:
use XML::Parser; # initialize parser and read the file $parser = new XML::Parser( Style => 'Tree' ); my $tree = $parser->parsefile( shift @ARGV ); # serialize the structure use Data::Dumper; print Dumper( $tree );
And here is the output:
$VAR1 = [ 'booklist', [ {}, 0, ' ', 'book', [ { 'type' => 'technical' }, 0, ' ', 'author', [ {}, 0, 'Book 1 author 1' ], 0, ' ', 'author', [ {}, 0, 'Book 1 author 2' ], 0, ' ', 'title', [ {}, 0, 'Book 1 title' ], 0, ' ', 'isbn', [ {}, 0, 'Book1ISBN' ], 0, ' ' ], 0, ' ', 'book', [ { 'type' => 'fiction' }, 0, ' ', 'author', [ {}, 0, 'Book 2 author 1' ], 0, ' ', 'author', [ {}, 0, 'Book 2 author 2' ], 0, ' ', 'title', [ {}, 0, 'Book 2 title' ], 0, ' ', 'isbn', [ {}, 0, 'Book2ISBN' ], 0, ' ' ], 0, ' ', 'book', [ { 'type' => 'technical' }, 0, ' ', 'author', [ {}, 0, 'Book 3 author 1' ], 0, ' ', 'author', [ {}, 0, 'Book 3 author 2' ], 0, ' ', 'author', [ {}, 0, 'Book 3 author 3' ], 0, ' ', 'title', [ {}, 0, 'Book 3 title' ], 0, ' ', 'isbn', [ {}, 0, 'Book3ISBN' ], 0, ' ' ], 0, ' ' ] ];
HOW TO ACCESS INDIVIDUAL ELEMENTS? How will they get stored in hash table? For example, I am interested in having "type" of the book.

Thanks in Advance,

Uday Sagar

Replies are listed 'Best First'.
Re: Accessing xml elements using XML::Parser module
by toolic (Bishop) on Feb 15, 2012 at 17:12 UTC
    For playing with Perl data structures, read: perldoc perldsc

    I used XML::Parser before I was aware of parsers which use method calls to extract information, such as XML::Twig. It was worth the investment in time for me to switch.

    use warnings; use strict; use XML::Twig; my $str = ' <booklist> <book type="technical"> <author>Book 1 author 1</author> <author>Book 1 author 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> </book> <book type="fiction"> <author>Book 2 author 1</author> <author>Book 2 author 2</author> <title>Book 2 title</title> <isbn>Book2ISBN</isbn> </book> <book type="technical"> <author>Book 3 author 1</author> <author>Book 3 author 2</author> <author>Book 3 author 3</author> <title>Book 3 title</title> <isbn>Book3ISBN</isbn> </book> </booklist> '; my $t = XML::Twig->new( twig_handlers => { book => \&book }, ); $t->parse($str); sub book { my ($t, $book) = @_; print $book->field('title'), ": "; print $book->att('type'), "\n"; } __END__ Book 1 title: technical Book 2 title: fiction Book 3 title: technical
      hey, its awesome! thank you. I have slightly modified my xml.
      <booklist> <book type="technical"> <author mainpage = "simple" finalpage = "style" ><author1>Book 1 + author1 1</author1></author> <author>Book 1 author 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> </book> <book type="fiction"> <author>Book 2 author 1</author> <author>Book 2 author 2</author> <title>Book 2 title</title> <isbn>Book2ISBN</isbn> </book> <book type="technical"> <author>Book 3 author 1</author> <author>Book 3 author 2</author> <author>Book 3 author 3</author> <title>Book 3 title</title> <isbn>Book3ISBN</isbn> </book> </booklist>
      how can i access author1 field now?
        how can i access author1 field now?
        • Read the documentation; I already provided the link to you.
        • Look at my code, and try to understand how it works.
        • Modify the code I provided; this change should be straightforward.
Re: Accessing xml elements using XML::Parser module
by Jenda (Abbot) on Feb 16, 2012 at 18:56 UTC

    You do not want to use XML::Parser directly. Have a look at XML::Rules, XML:Twig or XML::LibXML (they are not all built on top of XML::Parser, but that doesn't matter.) Choose the style that feels natural.

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