m0ve has asked for the wisdom of the Perl Monks concerning the following question:

hey monks,

i got an xml-file containing something like this :
<fall_liste> <fall ID="1"> <schein_liste> <schein ID="1"> <fk8000 V="0132"> <fk7199 V="12" BKZ="%!" /> <fk3000 V="134520" /> <fk3101 V="Doe" /> <fk3102 V="Joe" /> .....
my code looks like this :
my $twig= new XML::Twig(twig_handlers => { '/kvdt/datei_liste/datei +/fall_liste/fall' => \&fall } sub fall{ my( $twig, $element)= @_; my @scheine = $element->get_xpath('./schein_liste/schein/*/*'); for(@scheine){ <do various things> }
my question is how can i do something like
($element->name eq "fk3101")?(print "Fall : " . $element->{att}->{ID} . " name : " . $_->{att}->{V}):(); ?
$element->name is obviously wrong, how can i address that node name ? (inside the for(){})

Replies are listed 'Best First'.
Re: xml::twig and node names
by mirod (Canon) on Sep 18, 2007 at 08:07 UTC

    You mean you want something like this:

    foreach my $scheine (@scheine){ if( $scheine->name eq "fk3101") { print "Fall : " . $element->id . " name : " .$_->att( 'V') } }

    The important part is using foreach my $scheine (@scheine), name returns the name of the element, and I changed the way to output the message because I could not get myself to type it (or even cut-n-paste ;--) it the way you wrote it, it feels just wrong.

        .$_->att( 'V') doesn't work
        but ( $scheine->name eq "fk3101")?(print  " name : " . $scheine->{att}->{V}):();
        does what i want,
        thx for the hint on foreach =)
    Re: xml::twig and node names
    by m0ve (Scribe) on Sep 20, 2007 at 08:11 UTC
      well my script is done now but it's really slow.
      i made another one that just parses the xmlfiles as if they were just textfiles.
      that took about 4 seconds for 5 files, but using twig it takes 116 seconds...

      this is my code :
      ::twig
      sub readxml{ $xmlfile = shift; my $twig= new XML::Twig(twig_handlers => { '/kvdt' => \&fall }); + $twig->parsefile($xmlfile); } sub fall{ my( $twig, $element)= @_; my @scheine = $element->get_xpath('.//*'); foreach my $schein (@scheine){ my $fk = $schein->name; if ($schein->{att}->{USE}){($schein->{att}->{USE} =~ /EDV/)?(n +ext):();} if ($fk =~ /fk....\b/){ my $key = substr($fk,2,4); $data{xml}{$key}++; } } }
      text
      sub readxml{ $xmlfile = shift; open(XML, $xmlfile); while(<XML>){ chomp(); my $line = $_; my @bla; ($line =~ /USE="EDV"/)?(next):(); if ($line =~ /<fk....[^_]/){ @bla = split(/<fk/,$line); my $key = substr($bla[1],0,4); #30 $data{xml}{$key}++; } } close(XML); }
      is there a way to make this faster using ::twig ?
        i was able to improve it by using this :
        sub readxml{ $xmlfile = shift; my $twig= new XML::Twig( twig_handlers => { '_all_' => \&fall }, ); $twig->parsefile($xmlfile); } sub fall{ my( $twig, $element)= @_; if (!$element->{att}->{USE} || $element->{att}->{USE} !~ /EDV/){ if ($element->name =~ /fk....\b/){ my $key = substr($element->name,2,4); $data{xml}{$key}++; } } }
        but it still takes ~80 seconds...