I want to convert an xml file to csv. I found some code online and modified to my purpose, but I dont know enough about perl to further fix it.
use strict; use XML::Rules; use Text::CSV_XS; use FileHandle; my $csv = Text::CSV_XS->new({eol => "\n"}); my $parser = XML::Rules->new( rules => [ _default => 'content', Class => sub { $csv->print( $_[4]->{parameters}, [ map {$_[1]->{$_}} qw( Subject Course Title Descripti +on Prequisites Corequisites Requisites LectureHours LaboratoryHours C +reditHours Flags ) ]); return; } ] ); open my $FH, '>&STDOUT'; open my $File, 'alpha31.xml'; print $FH "Subject,Course,Title,Description,Prequisites,Corequisites,L +ectureHours,LaboratoryHours,CreditHours,Flags\n"; $parser->parse( $File, $FH);
And the brief contents of alpha31.xml
<Class> <Subject>AAH</Subject> <Course>119</Course> <Title>History of World Architecture I</Title> <Description>Comprehensive background as well as concentration on indi +vidual cultures and their architects from ancient to modern times. Di +scussion of architectures from around the world. Specific details and + expressions of more generalized theories and strategies will be explo +red.</Description> <LectureHours>3</LectureHours> <LaboratoryHours>0</LaboratoryHours> <CreditHours>3</CreditHours> <LectureHours>3</LectureHours> <LaboratoryHours>0</LaboratoryHours> <CreditHours>3</CreditHours> <Flags>(H)(C)</Flags> </Class> <Class> <Subject>AAH</Subject> <Course>120</Course> <Title>History of World Architecture II</Title> <Description>Comprehensive background as well as concentration on indi +vidual cultures and their architects from ancient to modern times. Di +scussion of architectures from around the world.Specific details and expressions of more generalized theories and strategies will be explor +ed.</Description> <LectureHours>3</LectureHours> <LaboratoryHours>0</LaboratoryHours> <CreditHours>3</CreditHours> <LectureHours>3</LectureHours> <LaboratoryHours>0</LaboratoryHours> <CreditHours>3</CreditHours> <Flags>(H)(C) <Flags> </Class> <Class> <Subject>AAH</Subject> <Course>301</Course> <Title>Thinking About Art</Title> <Description>A course designed for those who find art pleasing, meanin +gful or significant and who want to extend the range of their sensibi +lities. Theories of art will be studied for insight, as well as for h + istorical interest and continuity. Works of art will be studied for th +eir intrinsic value, for their relation to ideas and events, and as c +ultural artifacts. Regular visits to area museums and galleries will be required.</Description> <Prerequisite>HUM 102, 104, or 106.</Prerequisite> <LectureHours>3</LectureHours> <LaboratoryHours>0</LaboratoryHours> <CreditHours>3</CreditHours> <Flags>(H)(C)</Flags> </Class> <Class> <Subject>AAH</Subject> <Course>322</Course> <Title>19th Century American Art and Culture</Title> <Description>This course explores the artistic history of the United S +tates, from an agrarian society that developed into an industrialized + nation with a distinguished national art. This broad chronological s + urvey begins with the colonial art of Copley, Peale, West and Stuart, +followed by the nation building iconography of the Hudson River Schoo +l. The art of Mount and Bingham reflect antebellum culture, followed by Johnson in post-Civil War America on the eve of the Gilded Age. Fin +ally, the course examines the realism of Homer and Eakins, defining a + truly American iconography.</Description> <Prerequisite>HUM 102, 104, or 106.</Prerequisite> <LectureHours>3</LectureHours> <LaboratoryHours>0</LaboratoryHours> <CreditHours>3</CreditHours> <Flags>(H)(C)</Flags> </Class>
It converts it to xml the way I want to, but it only does a single line and then complains about garbage after the </class> tag. Could anyone help me have this work on the entire file. I have posted 4 entries from the xml file which should print out 4 csv rows.

In reply to xml to csv by jmno

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.