1

XML 的一部分如下所示:

<ipcEntry kind="1" symbol="A01B0013080000" ipcLevel="A" entryType="K" lang="EN" nocore="yes">
    <textBody>
        <title>
            <titlePart>
                <text>for working subsoil</text>
            </titlePart>
        </title>
    </textBody>
    <ipcEntry kind="2" symbol="A01B0013100000" ipcLevel="A" entryType="K" lang="EN" nocore="yes">
        <textBody>
            <title>
                <titlePart>
                    <text>Special implements for lifting subsoil layers</text>
                </titlePart>
            </title>
        </textBody>
        <ipcEntry kind="3" symbol="A01B0013120000" ipcLevel="A" entryType="K" lang="EN" nocore="yes">
            <textBody>
                <title>
                    <titlePart>
                        <text>Means for distributing the layers on the surface</text>
                    </titlePart>
                </title>
            </textBody>
        </ipcEntry>
    </ipcEntry>
</ipcEntry>

我的代码是:

#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
use Data::Dumper;

my $twig_handlers = { 'ipcEntry' =>  \&ipcEntrySub };

my $file = 'A01B.xml';
my $twig= new XML::Twig( twig_handlers => $twig_handlers );
$twig->parsefile($file);
#$twig->print;




sub ipcEntrySub {

   my ($twig_obj, $element) = @_;

  print $element->{'att'}->{'symbol'} . "\n";
 print "Kind: $element->{'att'}->{'kind'}\n";
 print $element->text . "\n";
 print "###########################################\n";


    $twig_obj->purge;

}

好像我无法得到文本:<text>Special implements for lifting subsoil layers</text> 我猜是因为<ipcEntry kind="2" symbol="A01B0013100000" ipcLevel="A" entryType="K" lang="EN" nocore="yes">有另一个子 ipcEntry。

我能得到<text>Means for distributing the layers on the surface</text>

我在这里做错了什么?

谢谢,

4

1 回答 1

6

来自 XML::Twig 文档:

flush 不应使用此方法,始终刷新树枝,而不是元素。

purge 与“flush”相同,只是它不打印树枝。它只是删除到目前为止已完全解析的所有元素。

将 purge 函数替换为 delete 函数以相反的顺序打印所有 ipcEntry 元素的文本,即。从最里面的 ipcEntry 元素开始。

sub ipcEntrySub {
  my ($twig_obj, $element) = @_;

  print $element->{'att'}->{'symbol'} . "\n";
  print "Kind: $element->{'att'}->{'kind'}\n";
  print $element->text . "\n";

  $element->delete;
}
于 2010-11-27T18:16:01.260 回答