Thread mit xml::twig module von txt file xml file generieren (7 answers)
Opened by osprofi at 2010-06-01 16:09

renee
 2010-06-01 17:36
#137854 #137854
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Ich würde das ungefähr so machen:
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
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my %hash = read_txt();

my $start = '00000';
my $xml   = get_xml( \%hash, $start );
print $xml;

sub get_xml {
    my ($hashref, $start) = @_;
    
    my $real_start = $hashref->{$start}->[0];
    return get_node( $hashref, $real_start, 0 );
}

sub get_node {
    my ($hashref,$name,$level) = @_;
    
    my $tab = "\t" x $level;
    
    my @children = map{ get_node( $hashref, $_, $level + 1 ) }@{ $hashref->{$name} || [] };
    if ( @children ) {
        my $children_string = join "\n", @children;
        return sprintf "%s<%s>\n%s\n%s</%s>", $tab, $name, $children_string, $tab, $name;
    }
     
    return sprintf "%s<%s/>", $tab, $name;
}

sub read_txt {
    my %parent_child_map;
    
    while ( my $line = <DATA> ) {
        chomp $line;
        next unless $line;
        my ($parent,$child) = split /\s+/, $line;
        
        push @{ $parent_child_map{ $parent } }, $child;
    }
    
    return %parent_child_map
}

__DATA__
00000 A0361
A0361 A0323
A0323 A0351
A0351 A0362
A0351 A0363
A0323 A0324
A0324 A0325
A0324 A0326
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread mit xml::twig module von txt file xml file generieren