如何处理同名的子节点?

发布于 2024-12-03 21:36:49 字数 545 浏览 6 评论 0原文

我有包含 1000 个实体的 xml,如下面的模板:

<data>
  <row> ded</row>
   <row> def </row>
   <row> fff </row>
   <row> fff </row>

</data>

我需要使用 XML::Twig 来解析它。

我使用以下代码:

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

  }
} );

如何遍历行并提取它们(它们具有相同的名称)?

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

<data>
  <row> ded</row>
   <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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-12-10 21:36:49

您可以使用 XML::Twig 方法 children_trimmed_text()< /a> - 它会给你一个子列表,然后你可以对其进行迭代。像这样的东西:

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

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";
    }
}
半透明的墙 2024-12-10 21:36:49
twig_handlers => {
#   '/data/row'
#   '//row'
#   'row'
   '//data/row'
          => sub { print $_->get_trimmed_text },
}
twig_handlers => {
#   '/data/row'
#   '//row'
#   'row'
   '//data/row'
          => sub { print $_->get_trimmed_text },
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文