开发者

How do I process child nodes with the same name?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜