Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

XML Twig sprint output not preserving > entity

by jccunning (Acolyte)
on Aug 22, 2012 at 14:43 UTC ( [id://989060]=perlquestion: print w/replies, xml ) Need Help??

jccunning has asked for the wisdom of the Perl Monks concerning the following question:

Following example of xml file that contains less than and greater than symbols as entities. When output through sprint, the > entity is converted to actual symbol. The < is left alone as desired. Is this bug or option I need to set to keep the > as indicated after BAR.
#!/usr/bin/perl -- use strict; use warnings; use XML::Twig; use Data::Dump qw' dd '; Main( @ARGV ); exit (0); sub Main { my %files; my $filename; my $subprint = sub { my( $twig, $_ ) = @_; push @{ $files{ $filename }}, $_->sprint; return; }; my $twig = XML::Twig->new( ignore_elts => { brief => 'discard', detailed => 'discard' }, pretty_print => 'indented', TwigHandlers => { 'myroot/classes' => $subprint, }, ); for my $file( @_ ) { $filename = $file; eval { $twig->parsefile( $file ); 1; } or warn "ERROR parsefile($file): $@ "; $twig->purge; } dd \%files; }
XML file:
<?xml version='1.0' standalone='yes'?> <myroot> <classes name="SomeClass::AccessLogic"> <public_typedefs> <members name="Ptr" kind="typedef" type="boost::shared_ptr&lt; + BAR &gt;" virtualness="non_virtual"> <brief></brief> <detailed></detailed> </members> </public_typedefs> </classes> </myroot>

Replies are listed 'Best First'.
Re: XML Twig sprint output not preserving &gt; entity
by Mr. Muskrat (Canon) on Aug 22, 2012 at 19:41 UTC

    You need to set keep_encoding.

    #!/usr/bin/perl -- use strict; use warnings; use XML::Twig; use Data::Dump qw' dd '; Main( @ARGV ); exit (0); sub Main { my %files; my $filename; my $subprint = sub { my( $twig, $_ ) = @_; push @{ $files{ $filename }}, $_->sprint; return; }; my $twig = XML::Twig->new( ignore_elts => { brief => 'discard', detailed => 'discard' }, keep_encoding => 1, pretty_print => 'indented', TwigHandlers => { 'myroot/classes' => $subprint, }, ); for my $file( @_ ) { $filename = $file; eval { $twig->parsefile( $file ); 1; } or warn "ERROR parsefile($file): $@ "; $twig->purge; } dd \%files; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://989060]
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-23 22:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found