#!/usr/bin/perl -- use warnings; use strict; use XML::Rules; use Text::CSV_XS; Main(@ARGV); exit(0); sub Main { use autodie; open my $File, shift || 'alpha31.xml'; RuleCvs( $File, \*STDOUT ); #~ use FileHandle; #~ RuleCvs( FileHandle->new(shift // 'alpha31.xml'), \*STDOUT ); use + 5.10.0; #~ RuleCvs( FileHandle->new(shift || 'alpha31.xml'), \*STDOUT ); #~ RuleCvs( FileHandle->new(shift), \*STDOUT ); } ## end sub Main BEGIN { my (@Heads) = qw( Subject Course Title Description Prequisites Corequisites Requisites LectureHours LaboratoryHours CreditHours Flags ); sub RuleCvs { my ( $InFh, $OutFh ) = @_; my $csv = Text::CSV_XS->new( { eol => "\n" } ); my $parser = XML::Rules->new( rules => [ _default => 'content', Class => sub { $csv->print( $_[4]->{parameters}, [ @{ $_[1] }{@Heads} ] ); return; } ] ); print $OutFh join( ',', @Heads ), "\n"; $parser->parse( $InFh, $OutFh ); return; } ## end sub RuleCvs } ## end BEGIN __END__

In reply to Re^2: xml to csv by Anonymous Monk
in thread 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.