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


In reply to Accessing xml elements using XML::Parser module by uday_sagar

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.