Thread XML::Twig Problem beim Parsen und ausgeben (7 answers)
Opened by Reigel at 2009-04-07 09:49

renee
 2009-04-07 10:44
#120337 #120337
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Das ist auch nicht sonderlich schwierig:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/perl

use strict;
use warnings;
use XML::Twig;
use Data::Dumper;

my $xml = do{ local $/; <DATA> };

my @hosts;
my $parser  = XML::Twig->new(
    twig_handlers => { 
        'HOST'       => \&hosts,
});
$parser->parse($xml);

print Dumper \@hosts;

sub hosts {
    my ($twig,$host) = @_;
    
    my %hash = (
        ip      => $host->findvalue( 'IP' ),
        dns     => $host->findvalue( 'DNS' ),
        comment => $host->findvalue( 'COMMENT/VALUE' ),
    );
    
    my @attrs = $host->findnodes( 'USER_DEFINED_ATTR_LIST/USER_DEFINED_ATTR' );
    
    for my $attr ( @attrs ) {
        my $index = $attr->findvalue( 'UDA_INDEX' );
        next unless $index == 2;
        
        $hash{attr} = $attr->findvalue( 'UDA_VALUE' );
    }
    
    push @hosts, \%hash;
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread XML::Twig Problem beim Parsen und ausgeben