1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<datas>
<data id="1">
<page>1</page>
</data>
<data id="2">
<page>2</page>
<data id="2.1">
<page>2</page>
<data id="2.1.1">
<page>2</page>
</data>
</data>
</data>
</datas>
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/perl
# use module
use XML::Simple;
use Data::Dumper;
$xml = new XML::Simple;
$data = $xml->XMLin("data.xml", KeyAttr => [], ForceArray => 1);
foreach $e (@{$data->{data}})
{
print "ida: ", $e->{id}, "\n";
print "idb: ", $e->{data}{id}, "\n";
}
1 2 3 4 5 6 7 8 9 10 11 12
... my @todo = @{$data->{data}}; # unsere Todo-liste als Stack while (@todo) { my $data = pop @todo; # eins runter print "id: ", $data->{id}, "\n"; if (exists $data->{data}) { # hier weiss ich nicht, was XML::Simple bei nur einem vorhandenen data macht, mal ausprobieren obs klappt push @todo, @{$data->{data}}; # die neuen datas auf unsere todo-liste } }
1 2 3 4 5 6 7 8 9 10
my @todos = map { { data => $_, level => 1 } } @{$data->{data}}; while (@todos) { my $todo = pop @todos; print "id: ", $todo->{data}{id}, "\n"; print "level: ", $todo->{level}, "\n"; if (exists $todo->{data}{data}) { push @todos, map { { data => $_, level => $todo->{level} +1 } } @{$todo->{data}{data}}; } }
1 2 3 4 5 6 7 8 9 10 11
my @todos = (); push @todos, { data => $_, level => 1 } for @{$data->{data}}; while (@todos) { my $todo = pop @todos; print "id: ", $todo->{data}{id}, "\n"; print "level: ", $todo->{level}, "\n"; if (exists $todo->{data}{data}) { push @todos, { data => $_, level => $todo->{level} +1 } for @{$todo->{data}{data}}; } }
1 2 3 4 5 6 7 8 9 10
sub exec_data { my ($data, $level) = @_; print "id: $data->{id}\n"; print "level: $level\n"; if (exists $data->{data}) { exec_data($_, $level +1) for @{$data->{data}}; } } exec_data($_, 1) for @{$data->{data}};
1 2 3 4
while (@todos) { my $todo = pop @todos; print "id: ", $todo->{data}{id}, "\n"; splice(@{$todo->{data}},0,1) if ($todo->{data}{id} =~ /^2$/);