Hello Perl Monks! I have an XML file similar to the following:

<booklist> <book> <author>Book 1 author 1</author> <author>Book 1 author 2</author> <title>Book 1 title</title> <isbn>Book1ISBN</isbn> </book> <book> <author>Book 2 author 1</author> <author>Book 2 author 2</author> <title>Book 2 title</title> <isbn>Book2ISBN</isbn> </book> <book> <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>

I would like to loop through this file and output each book's XML code including the opening and closing book tags. I've tried parsing the XML file with XML::Simple using XMLin and that gives me a data structure like so:

$VAR1 = { 'book' => [ { 'isbn' => 'Book1ISBN', 'title' => 'Book 1 title', 'author' => [ 'Book 1 author 1', 'Book 1 author 2' ] }, { 'isbn' => 'Book2ISBN', 'title' => 'Book 2 title', 'author' => [ 'Book 2 author 1', 'Book 2 author 2' ] }, { 'isbn' => 'Book3ISBN', 'title' => 'Book 3 title', 'author' => [ 'Book 3 author 1', 'Book 3 author 2', 'Book 3 author 3' ] } ] };

I'm really at a loss as how I would loop throught this to output the XML. I think the data structure is confusing me. I need the XML chunks to be identical to the input. I'm assuming I would use XMLout. Or maybe something completely different? Thanks in advance for any pointers!


In reply to XML::Simple XML / XMLin / XMLout? or something else? by Roboz

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.