in reply to XML::Twig parsing poorly structured content
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use XML::Twig; my $header; my $twig = 'XML::Twig'->new( twig_handlers => { h3 => sub { $header = $_->text; $_->purge; }, 'div[@class="event"]' => sub { say $header, "\t", $_->text; $_->purge; }, }, ); $twig->parsefile('file.xml');
In bigger projects, you don't want to have a global header. Instead, you can create a new class that has two attributes, header and twig, which delegates all the XML related work to the latter and stores the headers in the former.
#!/usr/bin/perl { package XML::Twig::WithHeader; use feature qw{ say }; use Moo; use XML::Twig; has _header => ( is => 'rw', init_arg => undef ); has _twig => ( is => 'lazy', init_arg => undef ); sub _build__twig { my ($self) = @_; my $twig = 'XML::Twig'->new( twig_handlers => { h3 => sub { $self->_header($_->text); $_->purge; }, 'div[@class="event"]' => sub { say $self->_header, "\t", $_->text; $_->purge; }, }, ); } sub parse { my ($self, $file) = @_; $self->_twig->parsefile($file); } } my $twig = 'XML::Twig::WithHeader'->new; $twig->parse('file.xml');
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: XML::Twig parsing poorly structured content
by slugger415 (Monk) on Jan 25, 2017 at 00:36 UTC | |
by choroba (Cardinal) on Jan 25, 2017 at 08:26 UTC | |
by kcott (Archbishop) on Jan 25, 2017 at 08:21 UTC | |
by slugger415 (Monk) on Jan 25, 2017 at 16:08 UTC |