Thread XML DOM/XPath/... ich komme nicht weiter (4 answers)
Opened by hans at 2011-02-05 19:30

pq
 2011-02-05 21:08
#145385 #145385
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
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
use 5.010;
use strict;
use warnings;
use XML::LibXML;

my $parser = XML::LibXML->new();
my $doc = $parser->parse_string(<<"EOM")->documentElement;
<threads>
  <thread>
    <date>11.22.3333</date>
    <name>Max Mustermann</name>
  </thread>
</threads>
EOM

sub walk {
    my ($node, $stack) = @_;
    my @children = $node->findnodes("*");
    for my $child (@children) {
        if (ref ($child) eq "XML::LibXML::Element") {
            walk($child, [@$stack, $node->nodeName])
        }
    }
    unless (@children) {
        say join "/", @$stack, $node->nodeName
    }
}
walk($doc, []);
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: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem

View full thread XML DOM/XPath/... ich komme nicht weiter