1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<Profile origin="get" version="2.1" date="2013.05.20">
<Catalog id="sys" version="2.1">
<Parameter name="prim" value="2"/>
<Parameter name="second" value="3"/>
<Parameter name="copy" value="4"/>
<Parameter name="force" value="1"/>
<Parameter name="always" value="0"/>
<Parameter name="type" value="5" Type="False"/>
<Parameter name="memory" value="allow"/>
</Catalog>
</profile>
1 2 3 4 5 6 7 8 9 10 11 12 13
use strict; use warnings; use XML::Twig; my $twig= XML::Twig->new( twig_handlers => { 'Parameter' => sub { $, = ","; print $_->{'att'}->{'name'}. $, } } ); $twig->parsefile('test.xml');
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8"?>
<Profile origin="get" version="2.0.1" date="2015-03-19T10:38:58Z">
<Catalog id="sysdumpdevParam" version="2.1">
<Parameter name="prim" value="/dev/lg_dumplv"/>
<Parameter name="second" value="/dev/sysdumpnull"/>
<Parameter name="copy" value="/var/adm/ras"/>
<Parameter name="forced" value="1"/>
<Parameter name="always" value="0"/>
<Parameter name="memory" value="disallow"/>
</Catalog>
</Profile>
twig2
fault
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
use strict; use warnings; use XML::Twig; my $FileResult = 'result.txt'; open( my $FhResult, '>', $FileResult ) or die("Unable to open file $FileResult\n$!"); my $twig1= XML::Twig->new( twig_handlers => { 'Parameter' => sub { $, = ","; print $FhResult $_->{'att'}->{'name'}. $, } } ); print $FhResult( (split('_', "d100spuptl25e0_datei1.xml",2))[0] . ',' ); $twig1->parsefile("d100spuptl25e0_datei1.xml"); my $twig2= XML::Twig->new( twig_handlers => { 'Parameter' => sub { $, = ","; print $FhResult $_->{'att'}->{'value'} . $, } } ); print $FhResult( (split('_', "\ndefault_datei2.xml",2))[0] . ',' ); $twig2->parsefile('default_datei2.xml');
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8"?>
<profile origin="get" version="2.0.1" date="2015-03-19T10:31:57Z">
<Catalog id="sysdumpdevParam" version="2.1">
<Parameter name="prim" value="/dev/lg_dumplv"/>
<Parameter name="second" value="/dev/sysdumpnull"/>
<Parameter name="copy" value="/var/adm/ras"/>
<Parameter name="forced" value="1"/>
<Parameter name="always" value="0"/>
<Parameter name="type" value="fw-assisted" applyType="nextboot" reboot="true"/>
<Parameter value="disallow"/>
</Catalog>
</profile>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
use strict; use warnings; use XML::Twig; my $FileResult = 'result.txt'; open( my $FhResult, '>', $FileResult ) or die("Unable to open file $FileResult\n$!"); my $twig1= XML::Twig->new( twig_handlers => { 'Parameter' => sub { $, = ","; print $FhResult $_->{'att'}->{'name'}. $, } } ); print $FhResult( (split('_', "d100spuptl25e0_datei1.xml",2))[0] . ',' ); $twig1->parsefile("d100spuptl25e0_datei1.xml");
1 2 3 4 5 6 7 8 9
my $twig1= XML::Twig->new( twig_handlers => { 'Parameter' => sub { # use 'fault' if name is not defined; Operator '//' is available since perl 5.10.1 my $attr_name = $_->{'att'}->{'name'} // 'fault'; print $FhResult $attr_name . ","; }, }, );
1 2 3 4 5 6 7 8 9 10 11 12
my $twig1= XML::Twig->new( twig_handlers => { 'Parameter' => sub { local $, = ","; my $item = $_; # copy "original" $_ to $item; because map uses $_ as well my @names = map { defined ? $_ : 'fault' } $item->{'att'}->{'name'}; print $FhResult @names, ""; }, }, );