Thread Teile aus XML Dokument extrahieren (16 answers)
Opened by BratHering at 2009-01-22 13:05

Linuxer
 2009-01-23 12:20
#118269 #118269
User since
2006-01-27
3870 Artikel
HausmeisterIn

user image
Hm, bisher habe ich ja die *::Parser Module vermieden. Irgendwie war mir die Art und Weise zu kryptisch, als dass sich mein Hirn da reindenken wollte.
murphys Hinweis hat nun mein Interesse geweckt und ich hab mich mal an einer Lösung versucht:

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
57
58
59
60
61
62
#! /usr/bin/perl
use strict;
use warnings;
use XML::Parser;

{
    my $is_article = 0;

    sub handle_start {
        my ( $expat, $element, %attr ) = @_;

        if ( $element eq 'article' ) {
            $is_article = 1;
        }
        elsif ( $is_article ) {
            print $expat->original_string;
        }
    }

    sub handle_end {
        my ( $expat, $element, %attr ) = @_;

        if ( $element eq 'article' ) {
            $is_article = 0;
        }
        elsif ( $is_article ) {
            print $expat->original_string;
        }
    }

    sub handle_char {
        my ( $expat, $string ) = @_;

        #print $expat->original_string if $is_article;
        print $string if $is_article;
    }
}


my $x = XML::Parser->new();

$x->setHandlers(
    Start => \&handle_start,
    End   => \&handle_end,
    Char  => \&handle_char,
);

$x->parse( *DATA );

__DATA__
<document>
    <article>
        Hello
        <foo>Foo</foo>
        World
    </article>
    <article>
        Hello
        <bar>Bar</bar>
        World
    </article>
</document>
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Teile aus XML Dokument extrahieren