Thread XML::DOM / XML::Semanticdiff: why oh why don't they DWIM (12 answers)
Opened by dukeofnukem at 2007-03-20 13:58

dukeofnukem
 2007-03-21 12:09
#75162 #75162
User since
2007-01-15
47 Artikel
BenutzerIn
[default_avatar]
Ok, here we go; hoffe es ist minimal genug *hüstel*

Zuerst der Input, hier sei nur ein Dokument vollständig genannt, das zweite enthält statt der city 'Meatball' die city 'Arglbargl':
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0"?>
<spam-document version="3.5" timestamp="2002-05-13 15:33:45">
<!-- Autogenerated by WarbleSoft Spam Version 3.5 -->
<customer>
<first-name>Joe</first-name>
<surname>Wrigley</surname>
<address>
<street>17 Beable Ave.</street>
<city>Meatball</city>
<state>MI</state>
<zip>82649</zip>
</address>
<email>joewrigley@jmac.org</email>
<age>42</age>
</customer>
</spam-document>

nun das diffscript:
Code: (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
#!/usr/bin/env perl

use strict;
use warnings;
use XML::DOM;
use XML::DOM::XPath;
use XML::SemanticDiff;

my ($file1, $file2) = @ARGV;

my $doc1 = do { local(*ARGV, $/, $_); @ARGV = $file1; <>; };
my $doc2 = do { local(*ARGV, $/, $_); @ARGV = $file2; <>; };

my $differ = XML::SemanticDiff->new(keepdata => 1,);
my @deltas = $differ->compare( $doc1, $doc2 );

my $majorDOMus = XML::DOM::Parser->new();
my ($tree1, $tree2) = map { $majorDOMus->parse($_); } $doc1, $doc2;

for my $delta (@deltas) {
my $path = $delta->{'context'};
my @nodes;

( push @nodes, $tree1->findnodes($path) ) ?
1 :
( push @nodes, $tree2->findnodes($path) ) ?
1 :
print "\nThere is no node at '$path'\n";

if (@nodes) {
print "\nNODEVALUE(getNodeValue): ", $_->getNodeValue for @nodes;
print "\nNODEVALUE(getData): ", $_->XML::DOM::CharacterData::getData for @nodes;
print "\nTOSTRING: ", $_->toString, "\n" for @nodes;
}
}

und die dazugehörige Ausgabe:
Code: (dl )
1
2
3
4
Use of uninitialized value in print at ./TestGetValue line 31.
NODEVALUE(getNodeValue):
NODEVALUE(getData): city
TOSTRING: <city>Meatball</city>


Hoffe das ist nicht zu unübersichtlich...

Wieso liefert getNodeValue uninitialized?

Wieso liefert XML::DOM::CharacterData::getData das Tag statt des Inhalts?

TIA,

Martin\n\n

<!--EDIT|dukeofnukem|1174471995-->
drum&bass is a state of mind

View full thread XML::DOM / XML::Semanticdiff: why oh why don't they DWIM