Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: XML::Twig and file encoding

by Anonymous Monk
on Jul 30, 2014 at 19:05 UTC ( [id://1095653]=note: print w/replies, xml ) Need Help??


in reply to XML::Twig and file encoding

... somethings wrong ...

Great :) prove it

Finish this program, post it, and post the output

#!/usr/bin/perl -- use Path::Tiny qw/ path /; use Data::Dump qw/ dd /; my $infile = 'three-lines-unicode.xml'; my $outfile = 'three-lines-uniWHAT.xml'; dd( path( $infile )->slurp_utf8 ); getTwiggy( $infile , $outfile ); dd( path( $outfile )->slurp_raw ); sub getTwiggy { ## minimal demo of Twig not what you want } __END__

But you probably don't want to use http://xmltwig.org/xmltwig/twig_dev.html#METHODS_XML_Twig_new_keep_encoding This is a (slightly?) evil option: if the XML document is not UTF-8 encoded

xmltwig.org keep-encoding site:xmltwig.org keep-encoding

Replies are listed 'Best First'.
Re^2: XML::Twig and file encoding
by slugger415 (Monk) on Jul 31, 2014 at 17:42 UTC

    getting an error, maybe I coded something wrong here. (Running on Win7 BTW - should've mentioned.)

    Error open (<:unix) on 'twiggy-WHAT.xml': No such file or directory at C:\working\scripts\ifx-make-conrefs\twiggy.pl line 9.

    my code

    #!/usr/bin/perl -- use Path::Tiny qw/ path /; use Data::Dump qw/ dd /; 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 { ## minimal demo of Twig not what you want } __END__

    I do get my XML printed to screen.

    thanks for looking at this.

      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?

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1095653]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found