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;

}

テキストを取得できないようです。別のサブipcEntryが<text>Special implements for lifting subsoil layers</text> あるためだと思います。<ipcEntry kind="2" symbol="A01B0013100000" ipcLevel="A" entryType="K" lang="EN" nocore="yes">

私は得ることができます<text>Means for distributing the layers on the surface</text>

私はここで何を間違っているのですか?

ありがとう、

4

1 に答える 1

6

XML :: Twigドキュメントから:

フラッシュこの方法は使用しないでください。要素ではなく、常に小枝をフラッシュしてください。

パージ小枝を印刷しないことを除いて、「フラッシュ」と同じことを行います。これまでに完全に解析されたすべての要素を削除するだけです。

パージ関数を削除関数に置き換えると、すべての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 に答える