Leser: 11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<HOST>
<IP>192.168.1.1</IP>
-<TRACKING_METHOD>
<VALUE>IP address</VALUE>
</TRACKING_METHOD>
<DNS>dns-server.test.de</DNS>
<OPERATING_SYSTEM>Linux</OPERATING_SYSTEM>
-<COMMENT>
<VALUE>Testnetz</VALUE>
</COMMENT>
-<USER_DEFINED_ATTR_LIST>
-<USER_DEFINED_ATTR>
<UDA_INDEX>1</UDA_INDEX>
<UDA_TITLE>Ansprechpartner</UDA_TITLE>
<UDA_VALUE>Herr Mustermann</UDA_VALUE>
</USER_DEFINED_ATTR>
-<USER_DEFINED_ATTR>
<UDA_INDEX>2</UDA_INDEX>
<UDA_TITLE>Standort</UDA_TITLE>
<UDA_VALUE>Testnetz</UDA_VALUE>
</USER_DEFINED_ATTR>
</USER_DEFINED_ATTR_LIST>
</HOST>
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#!/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' ), ); push @hosts, \%hash; } __DATA__ <root> <HOST> <IP>192.168.1.1</IP> <TRACKING_METHOD> <VALUE>IP address</VALUE> </TRACKING_METHOD> <DNS>dns-server.test.de</DNS> <OPERATING_SYSTEM>Linux</OPERATING_SYSTEM> <COMMENT> <VALUE>Testnetz</VALUE> </COMMENT> <USER_DEFINED_ATTR_LIST> <USER_DEFINED_ATTR> <UDA_INDEX>1</UDA_INDEX> <UDA_TITLE>Ansprechpartner</UDA_TITLE> <UDA_VALUE>Herr Mustermann</UDA_VALUE> </USER_DEFINED_ATTR> <USER_DEFINED_ATTR> <UDA_INDEX>2</UDA_INDEX> <UDA_TITLE>Standort</UDA_TITLE> <UDA_VALUE>Testnetz</UDA_VALUE> </USER_DEFINED_ATTR> </USER_DEFINED_ATTR_LIST> </HOST> </root>
1
2
3
4
5
6
7
8
$xml = new XML::Twig(
TwigHandlers => {
HOST => \&getHost_Host,
#COMMENT => \&getHost_Comment,
#USER_DEFINED_ATTR => \&getHost_USER_DEFINED_ATTR,
},
comments => 'keep',
);
1
2
3
4
5
6
7
8
9
10
11
12
<USER_DEFINED_ATTR_LIST>
<USER_DEFINED_ATTR>
<UDA_INDEX>1</UDA_INDEX>
<UDA_TITLE>Ansprechpartner</UDA_TITLE>
<UDA_VALUE>Herr Mustermann</UDA_VALUE>
</USER_DEFINED_ATTR>
<USER_DEFINED_ATTR>
<UDA_INDEX>2</UDA_INDEX>
<UDA_TITLE>Standort</UDA_TITLE>
<UDA_VALUE>Testnetz</UDA_VALUE>
</USER_DEFINED_ATTR>
</USER_DEFINED_ATTR_LIST>
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; }