in reply to Re^2: XML::Twig and file encoding
in thread XML::Twig and file encoding

You missed the point :) You didn't finish the program :) you did not complete sub getTwiggy which is supposed to create $outfile

How else would I know what you're doing with XML::Twig and what goes wrong?

Replies are listed 'Best First'.
Re^4: XML::Twig and file encoding
by slugger415 (Monk) on Aug 01, 2014 at 17:08 UTC

    ok sorry, brain cramp!

    #!/usr/bin/perl -- use Path::Tiny qw/ path /; use Data::Dump qw/ dd /; use XML::Twig; my $infile = 'twiggy-test.xml'; my $outfile = 'twiggy-WHAT.xml'; dd( path( $infile )->slurp_utf8 ); getTwiggy( $infile , $outfile ); dd( path( $outfile )->slurp_raw ); sub getTwiggy { # set up the XML parser: my $twig= XML::Twig->new( comments => 'keep', # keep_encoding => 1, keep_spaces=> 1, twig_handlers => { li => sub_ix_strings($infile), }, ); $twig->parsefile($infile); print " Saving $outfile...\n"; open(NEW,">$outfile") || die("cannot open file $outfile for writing +.\n"); $twig->flush( \*NEW); close(NEW); } sub sub_ix_strings { my($file) = @_; return sub { my($twig,$elt) = @_; $elt->set_att('test',"hello"); } } __END__

    OUTPUT (looks the same, but file is now ANSI not UTF-8):

    <?xml version="1.0" encoding="UTF-8"?> <reference id="ids_excal_142" xml:lang="en-us"> <title>The ISO character set</title> <refbody> <section><p>Each ISO character has its own value, except that lowercas +e characters are translated to uppercase. Two characters that have no uppercase equivalent are: <ul> <li test="hello">German small sharp s (ß - 0xDF)</li> <li test="hello">Lowercase y diaeresis/umlaut (ÿ - 0xFF)</li> </ul></p></section> </refbody> </reference>

    So the output looks ok in my text editor (TextPad) but when I open it in an XML editor (Arbortext or Oxygen) the special characters don't display correctly.

    Thanks! Scott

      Also this is what's printed to STDOUT:

      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<reference id=\"ids_exc +al_142\" xml:lang=\"en-us\">\r\n<title>The ISO character set</title>\ +r\n<refbody>\r\n<section><p>Each ISO character has its own value, exc +ept that lowercase\r\ncharacters are translated to uppercase. Two cha +racters that have no\r\nuppercase equivalent are: <ul>\r\n<li test=\" +hello\">German small sharp s (\xDF - 0xDF)</li>\r\n<li test=\"hello\" +>Lowercase y diaeresis/umlaut (\xFF - 0xFF)</li>\r\n</ul></p></sectio +n>\r\n</refbody>\r\n</reference>"