uday_sagar has asked for the wisdom of the Perl Monks concerning the following question:
Here is my perl code to parse it:<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>
And here is the output: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 );
HOW TO ACCESS INDIVIDUAL ELEMENTS? How will they get stored in hash table? For example, I am interested in having "type" of the book.$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, ' ' ] ];
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 | |
by uday_sagar (Scribe) on Feb 16, 2012 at 04:58 UTC | |
by toolic (Bishop) on Feb 16, 2012 at 14:35 UTC | |
|
Re: Accessing xml elements using XML::Parser module
by Jenda (Abbot) on Feb 16, 2012 at 18:56 UTC |