开发者

How do I process child nodes with the same name?

开发者 https://www.devze.com 2023-04-04 03:00 出处:网络
I have xml with the 1000 entities like the below template : <data> <row> ded</r开发者_JAVA百科ow>

I have xml with the 1000 entities like the below template :

<data>
  <row> ded</r开发者_JAVA百科ow>
   <row> def </row>
   <row> fff </row>
   <row> fff </row>

</data>

I need to parse it with XML::Twig.

I use the following code:

my $twig = XML::Twig->new(
twig_handlers => {
  data => sub {

        my $x1 = $_->first_child_trimmed_text('row');
        print $x1;
        #I need also here to run over the other rows and extract them

  }
} );

How can I run over the rows and extract them (they have the same name)?


You can use the XML::Twig method children_trimmed_text() - it'll give you a list of the children, which you can then iterate over. Something like this:

data => sub {
    my @row_children = $_->children_trimmed_text( 'row' );
    for my $row ( @row_children ) {
       print "$row\n";
    }
}


twig_handlers => {
#   '/data/row'
#   '//row'
#   'row'
   '//data/row'
          => sub { print $_->get_trimmed_text },
}
0

精彩评论

暂无评论...
验证码 换一张
取 消