It looks like there is a problem with the module, either with the code or with the docs. For such problems with uncommon XML modules you should try asking on the perl-xml mailing list (Kip usually reads it). If you don't get an answer you can then try contacting the author and/or reporting the bug through rt.cpan.org.

In this case I would probably use a different module though. See below for a bunch of possibilities (there are many more, from XML::LibXML to XML::SAX::Writer):

#!/usr/bin/perl -w use strict; use Test::More qw(no_plan); use XML::SemanticDiff; use File::Slurp; use Getopt::Std; my %opt; # -v is verbose, it displays the generated string for each test getopts( 'v', \%opt); # XML::SemanticDiff seems to only compare files, not strings my $tmp_expected= "tmp.expected"; # temp file with expected result my $tmp_result = "tmp.result"; # temp file with result my %tests=( 'XML::Simple' => { sub => \&test_xml_simple, }, 'XML::Writer' => { extra_modules => [ 'IO::Scalar' ], sub => \&test_xml_writer, }, 'XML::Handler::YAWriter' => { sub => \&test_xml_handler_yawriter, + }, 'XML::Twig' => { sub => \&test_xml_twig, }, ); write_file( $tmp_expected, <DATA>); my $sem_diff= XML::SemanticDiff->new; foreach my $module ( keys %tests) { my $test= $tests{$module}; my @modules= ($module); push @modules, @{$test->{extra_modules}} if( $test->{extra_modules +}); if( load_modules( @modules)) { my $result= $test->{sub}->(); write_file( $tmp_result, $result); my $diff= $sem_diff->compare( $tmp_expected, $tmp_result); ok( ! $diff, $module) or diag( "got $result") ; diag( "\n", $result, "\n\n") if( $opt{v}); } else { SKIP: { skip( "$module not available", 1); } } } unlink $tmp_expected, $tmp_result; sub test_xml_simple { my $xml= { entry => { att1 => 'value1', att2 => 'value2', path => +{ content => 'path & text'} } }; return XMLout( $xml, keeproot => 1); } sub test_xml_writer { my $result; my $output = IO::Scalar->new( \$result); my $writer = XML::Writer->new(OUTPUT => $output); $writer->startTag( entry => att1 => 'value1', att2 => 'value2'); $writer->dataElement( path => 'path & text'); $writer->endTag( 'entry'); $writer->end(); $output->close(); return $result; } sub test_xml_handler_yawriter { my $writer= XML::Handler::YAWriter->new( AsString => 1); $writer->start_document; $writer->start_element( { Name => 'entry', Attributes => { att1 => + 'value1', att2 => 'value2'} } ); $writer->start_element( { Name => 'path' } ); $writer->characters( { Data => 'path & text' } ); $writer->end_element( { Name => 'path'} ); $writer->end_element( { Name => 'entry'} ); return $writer->end_document; } sub test_xml_twig { my $root= XML::Twig::Elt->new( entry => { att1 => 'value1', att2 = +> 'value2' }); $root->insert_new_elt( path => "path & text"); return $root->sprint; } # a simple way of importing modules at run time #sub load_modules # { foreach (@_) # { if(eval "require $_") { import $_; } # else { return 0; } # } # return 1; # } # fun way sub load_modules { ((eval "require $_" and import $_ or 1) or return 0) foreach (@_) +; 1} __DATA__ <entry att1="value1" att2="value2"> <!-- the &amp; is here to test escaping --> <path>path &amp; text</path> </entry>

In reply to Re: XML::Generator::PerlData and attributes by mirod
in thread XML::Generator::PerlData and attributes by dda

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.