Thread xml aus array schreiben
(10 answers)
Opened by protag at 2010-06-17 12:29 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 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 Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wie frage ich & perlintro brian's Leitfaden für jedes Perl-Problem |