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
<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>
adConvert( 'XML', $self->{xmlFile}, 'CSV', $self->{output_file_csv}, $flags );
1
2
firstName,telephoneNumber,device,pattern,routePartitionName,name,userRoles
Sebastian,100,,,,,,
1
2
3
4
firstName,telephoneNumber,device,pattern,routePartitionName,name,userRoles
Sebastian,100,SEP111111111111,100,PT_Intern,Standard CTI Enabled,Standard CTI Enabled,
,,CSFBLA,,,Standard CCM End Users,Standard CCM End Users
,,SEP222222222222,,,,Standard CCMUSER Administration
2014-01-17T08:37:16 biancaIch nutze für XML sehr gern XML::Simple und dafür gibt es auch XML::Simple::Tree mit dem man sich durch die Struktur bewegen kann.
QuoteI find generally that XML::Simple ain't. Try XML::TreeBuilder or XML::Twig instead
QuoteGrundsätzlich ist es ja außerdem so, daß XML im Prinzip unendlich verschachtelt sein kann. Man kann daher durchaus nicht jeden XML-Code als CSV darstellen, bzw. in CSV umwandeln.
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
#!/usr/bin/env python # coding: iso-8859-1 import xml.dom.minidom xmlstring = """<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>""" suitablestring = "" for i in xmlstring: i = i.strip() suitablestring += i dom = xml.dom.minidom.parseString(suitablestring) def dokument(domina): for node in domina.childNodes: if node.nodeType == node.ELEMENT_NODE: print node.nodeName if node.nodeType == node.TEXT_NODE: print "Value: " + node.nodeValue.strip() print dokument(node) dokument(dom)
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
user
firstName
Value: Sebastian
telephoneNumber
Value: 100
associatedDevices
device
Value: SEP111111111111
device
Value: CSFBLA
device
Value: SEP222222222222
primaryExtension
pattern
Value: 100
routePartitionName
Value: PT_Intern
associatedGroups
userGroup
name
Value: StandardCTIEnabled
userRoles
userRole
Value: StandardCTIEnabled
userGroup
name
Value: StandardCCMEndUsers
userRoles
userRole
Value: StandardCCMEndUsers
userRole
Value: StandardCCMUSERAdministration
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);