1
2
3
4
5
6
7
0;0;Systemebene
1;1;Gruppe 1
2;1;Gruppe 1
5;2;Gruppe 2
5;2;Gruppe 2
601;3;Gruppe 3
60100;4;Teil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<map version="0.8.0">
<node TEXT="System">
<node TEXT="Gruppe1"/>
<node TEXT="Gruppe1">
<node TEXT="Gruppe 2"/>
<node TEXT="Gruppe 2">
<node TEXT="Gruppe 3">
<node TEXT="Teil 4"/>
</node>
</node>
</node>
</node>
</map>
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
use strict; use warnings; use XML::LibXML; use Text::CSV_XS; my $csv = Text::CSV_XS->new({ sep_char => ';' }); my $dom = XML::LibXML::Document->new; my $root = $dom->createElement( "map" ); $dom->setDocumentElement( $root ); $root->setAttribute(version => '0.8.0'); my @stack = $root; my $current_level = -1; while (my $line = <DATA>) { chomp $line; my $status = $csv->parse($line); # ggfs. fehlerabfrage my ($dummy, $level, $name) = $csv->fields; if ($level <= $current_level) { @stack = @stack[0 .. $level-1]; } my $parent = $stack[-1]; my $el = $parent->addNewChild( undef, "node" ); $el->setAttribute(TEXT => $name); $stack[$level] = $el; $current_level = $level; } my $string = $dom->toString(); print $string; __DATA__ 0;0;Systemebene 1;1;Gruppe 1a 2;1;Gruppe 1b 5;2;Gruppe 2a 5;2;Gruppe 2b 601;3;Gruppe 3 60100;4;Teil 4
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
#!/usr/bin/perl use strict; use warnings; use IO::Handle; my $stream = IO::Handle->new; die "Unable to open __DATA__." unless $stream->fdopen(fileno(DATA), "r"); &write_document_start; &traverse( $stream ); &write_document_end; sub traverse { my ($stream, $line) = @_; $line = $stream->getline unless defined $line; while(defined $line) { chomp $line; my ($id, $level, $text) = split /;/, $line; my $next = $stream->getline; my $is_empty = not defined $next; unless($is_empty) { chomp $next; my ($id2, $level2, $text2) = split /;/, $next; $is_empty = $level2 == $level; } &write_node($id, $level, $text, $is_empty); unless( $is_empty ) { &traverse( $stream, $next ); &write_node_end( $level ); $line = $stream->getline; } else { $line = $next; } } } sub write_node { my ($id, $level, $text, $is_empty) = @_; my $ident = "\t" x $level; print qq~$ident<node ID="$id" TEXT="$text"~; print " /" if $is_empty; print ">\n"; } sub write_node_end { my ($level) = @_; my $ident = "\t" x $level; print "$ident</node>\n"; } sub write_document_start { print qq~<map version="0.9.0">\n~; } sub write_document_end { print qq~</map>\n~; } __DATA__ 0;0;Systemebene 1;1;Gruppe 1 2;1;Gruppe 1 5;2;Gruppe 2 5;2;Gruppe 2 601;3;Gruppe 3 60100;4;Teil