Thread PHP-Script in Perl umwandeln (6 answers)
Opened by Henri at 2015-03-30 09:19

topeg
 2015-03-30 23:26
#180475 #180475
User since
2006-07-10
2611 Artikel
BenutzerIn

user image
Hier ein Beispiel wie man es machen kann:
more (27.7kb):
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use strict;
use warnings;
use XML::Simple;
use Data::Dumper;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 0;

my $xml1 = XMLin("test1.xml",ForceArray => 1, KeyAttr => [],ValueAttr => [],KeepRoot => 1) or die("Error: Cannot load XML1 ($!)");
my $xml2 = XMLin("test2.xml",ForceArray => 1, KeyAttr => [],ValueAttr => [],KeepRoot => 1) or die("Error: Cannot load XML2 ($!)");

my @Differences;

###
# wir arbeiten uns durch den Baum:
my @stack=({path => 'ROOT', childs1 => [$xml1], childs2 => [$xml2] });

while(@stack) {
  my $elm = shift(@stack);
  my $cnt = -1;

  if(ref($elm->{childs1}) eq 'ARRAY') {
    if(!ref($elm->{childs2}) eq 'ARRAY') {
      push(@Differences,"In $elm->{path} XML1 hat Kinder XML2 nicht!");
      next;
    }

    if($#{$elm->{childs1}} != $#{$elm->{childs2}}) {
      push(@Differences,"In $elm->{path} Kinder haben unterschiedliche Anzahl");
    }

    for my $child1 (@{$elm->{childs1}}) {
      my $child2 = $elm->{childs2}->[++$cnt];

      unless($child2) {
        push(@Differences,"In $elm->{path}[$cnt] aus XML1 hat keinen Partner in XML2");
        next;
      }

      if(ref($child1) eq 'HASH') {
        for my $key (sort keys(%$child1)) {
          if(!exists($child2->{$key})) {
            push(@Differences,"In $elm->{path}[$cnt]/$key aus XML1 hat keine entsprechung in XML2");
            next();
          }
          my $c1 = $child1->{$key};
          my $c2 = $child2->{$key};

          # Spezialbehandlung für "Parameter"
          # Die Attribute "name" und "value" bestimmen den Inhalt.
          if($key eq 'Parameter') {
            my $ok = 0;
            my %e2;

            for my $elm (@$c2) {
              next if(!defined($elm->{name}));
              next if(!defined($elm->{value}));
              $e2{$elm->{name}} = $elm->{value};
            }

            my $c = -1;
            for my $e1 (@$c1) {
              $c++;
              next if(!defined($e1->{name}));
              next if(!defined($e1->{value}));
              $ok++;
              push(@stack,{path => $elm->{path}.qq([$cnt]/$key).qq([$c] name=").$e1->{name}.qq("/value), childs1 => $e1->{value}, childs2 => $e2{$e1->{name}} });
            }

            # wenn das Aktiv ist wird die Reihenfolge der "Parameter" nicht geprüft
            #next if($ok);
          }

          if(ref($c1) and ref($c1) eq ref($c2) and ref($c1) ne 'ARRAY') {
            $c1 = [ $c1 ];
            $c2 = [ $c2 ];
          }
          push(@stack,{path => $elm->{path}."[$cnt]/$key", childs1 => $c1, childs2 => $c2 })
        }
        next;
      }

      if(Dumper($child1) ne Dumper($child2)) {
        push(@Differences,"In $elm->{path}[$cnt] aus XML1 hat einen anderen Wert als XML2 ($child1 <> $child2)");
      }
    }
    next;
  }

  if(Dumper($elm->{childs1}) ne Dumper($elm->{childs2})) {
    push(@Differences,"In $elm->{path} aus XML1 hat einen anderen Wert in XML2 ($elm->{childs1} <> $elm->{childs2})");
  }
}


open(my $fh, '>', "result.txt") or die("ERROR open ($!)");
if(@Differences) {
  print $fh join("\n",@Differences);
}
else {
  print $fh "Datei XML2 hat die selben Einträge wie XML1\n";
}
close($fh);


Aber Vorsicht. Wie auch in deinem Code wird nur geprüft ob ein Element aus XML1 auch in XML2 vorhanden ist und den selben Wert hat. XML2 kann noch zusätzliche Einträge haben.

View full thread PHP-Script in Perl umwandeln