in reply to xml to csv

You've encountered the silly "each XML document must have one and only one root tag" restriction. Let's see what do the XML::Rules docs say about that:

If you need to parse a XML file without the top-most tag (something that each and any sane person would allow, but the XML comitee did not), you can parse
<!DOCTYPE doc [<!ENTITY real_doc SYSTEM "$the_file_name">]><doc>&rea +l_doc;</doc>
instead.

And the

map {$_[1]->{$_}} qw( Subject Course Title Description Prequisites Corequisites Requisites LectureHours LaboratoryHours CreditHours Flags )
is better written
@{$_[1]}{qw( Subject Course Title Description Prequisites Corequisites Requisites LectureHours LaboratoryHours CreditHours Flags )}
No need to map(), just slice the hash.

You may also want to add the stripspaces => 3 to ensure the whitespace around the <Class> tags is not being accumulated in the $_[1]->{_content} for the handler of the root tag.

Jenda
Enoch was right!
Enjoy the last years of Rome.

Replies are listed 'Best First'.
Re^2: xml to csv
by Anonymous Monk on Nov 04, 2009 at 14:43 UTC
    #!/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__