Thread unbekanntes XML-File in CSV (8 answers)
Opened by mordur. at 2014-01-16 14:45

hlubenow
 2014-01-21 01:36
#173085 #173085
User since
2009-02-22
876 Artikel
BenutzerIn
[default_avatar]
Lustig, der (altmodische) Python-Weg oben geht auch in Perl, mit XML::DOM:
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
#!/usr/bin/perl

use warnings;
use strict;

use XML::DOM;

my $xmlstring = <<END;
<user>
   <firstName>Sebastian</firstName>
   <telephoneNumber>100</telephoneNumber>
   <associatedDevices>
                  <device>SEP111111111111</device>
                  <device>CSFBLA</device>
                  <device>SEP222222222222</device>
   </associatedDevices>
   <primaryExtension>
                  <pattern>100</pattern>
                  <routePartitionName>PT_Intern</routePartitionName>
   </primaryExtension>
   <associatedGroups>
            <userGroup>
                     <name>Standard CTI Enabled</name>
                     <userRoles>
                        <userRole>Standard CTI Enabled</userRole>
                     </userRoles>
            </userGroup>
            <userGroup>
                     <name>Standard CCM End Users</name>
                     <userRoles>
                        <userRole>Standard CCM End Users</userRole>
                        <userRole>Standard CCMUSER Administration</userRole>
                     </userRoles>
            </userGroup>
    </associatedGroups>
</user>
END

my @xml = split("\n", $xmlstring);
my $suitablestring = "";

for my $i (@xml) {
    $i = strip($i);
    $suitablestring .= $i;
}

my $parser = new XML::DOM::Parser;
my $dom = $parser->parsestring($suitablestring);

sub strip { 
    my $a = shift;
    $a =~ s/^\s+//;
    $a =~ s/\s+$//;
    return $a;
}

sub dokument {
    my $domina = shift;
    my @childNodes = $domina->getChildNodes();
    for my $node (@childNodes) {
        if ($node->getNodeType() == ELEMENT_NODE) {
            print $node->getNodeName() . "\n";
        }
        if ($node->getNodeType() == TEXT_NODE) {
            print "Value: " . strip($node->getNodeValue()) . "\n\n";
        }
        &dokument($node);
    }
}
&dokument($dom);

View full thread unbekanntes XML-File in CSV