簡體   English   中英

perl的簡單XML問題-如何檢索特定元素

[英]simple XML question for perl - how to retrieve specific elements

我試圖找出如何遍歷XML的方法,但我學到了很多東西,但仍然陷入困境。 這是信息:

我正在使用wordnik api使用XML :: Simple檢索XML:

 $content = get($url);
 $r = $xml->XMLin("$content");

實際的XML如下所示:

<definitions>
−
<definition sequence="0" id="0">
−
<text>
To withdraw one's support or help from, especially in spite of duty, allegiance, or responsibility; desert:  abandon a friend in trouble. 
</text>
<headword>abandon</headword>
<partOfSpeech>verb-transitive</partOfSpeech>
</definition>
−
<definition sequence="1" id="0">
−
<text>
To give up by leaving or ceasing to operate or inhabit, especially as a result of danger or other impending threat:  abandoned the ship. 
</text>
<headword>abandon</headword>
<partOfSpeech>verb-transitive</partOfSpeech>
</definition>
−
<definition sequence="2" id="0">
−
<text>
To surrender one's claim to, right to, or interest in; give up entirely. See Synonyms at relinquish.
</text>
<headword>abandon</headword>
<partOfSpeech>verb-transitive</partOfSpeech>
</definition>
−
<definition sequence="3" id="0">

...

我想要的只是FIRST定義的一部分。 我正在使用此代碼,但它得到的是LAST定義的POS:

    if($r->{definition}->{0}->{partOfSpeech}) {
      $pos = $r->{definition}->{0}->{partOfSpeech};
     }
else { $pos = $r->{definition}->{partOfSpeech}; }

我為此感到很尷尬,因為我知道有一種明顯更好的方法。 我很想得到像這樣簡單的操作,因此我可以更廣泛地遍歷所有元素。 但是,它對我不起作用(不知道要引用什么)。 我嘗試了以下多種變體-這只是我的最后一次嘗試:

 while (my ($k, $v) = each %{$r->{definitions}->{definition}[0]->{sequence}->{partOfSpeech}}) {
  $v =~ s/'/'"'"'/g;
  $v = "'$v'";
  print "export $k=$v\n";
 }

最后,當我執行“ print Dumper($ r)”時,它會顯示以下信息:

$VAR1 = {
          'definition' => {
                          '0' => {
                                 'partOfSpeech' => 'noun',
                                 'sequence' => '6',
                                 'text' => 'A complete surrender of inhibitions.',
                                 'headword' => 'abandon'
                               }
                        }
        };

(並且您看到的“名詞”是最后一個(第六個)定義/ partofspeech元素)。


根據以下RC的答案,我的新代碼如下所示:

$content = get($url);
$r = $xml->XMLin("$content", KeyAttr => { definition => 'sequence'});
while (my ($k, $v) = each %{$r->{definition}}) {
    $v=$r->{definition}->{$k}->{partOfSpeech};
    print "export $k=$v\n";
}

打印出以下內容:

export 6='noun'
export 4='verb-transitive'
export 1='verb-transitive'
export 3='verb-transitive'
export 0='verb-transitive'
export 2='verb-transitive'
export 5='noun'

因此,這很好,並且可以導出正確的對。 但是現在的問題是訂單被取消了(這很可能是Wordnik的問題,而不是編程問題)。 如何按鍵排序? 像這樣嗎

sort($r->{definition});

來自XML :: Simple doc:

注1:'KeyAttr'的默認值為['name','key','id']。 如果您不想在輸入上折疊或在輸出上展開,則必須將此選項設置為空列表以禁用該功能。

我認為在XMLin選項中添加KeyAttr => { definition => 'sequence' }可能會解決您的問題。

也可以使用XML :: Twig為您遍歷文件並幫助提取數據:

use XML::Twig;

my $content = do { local $/; <DATA> };      # get data

XML::Twig->new(twig_handlers => {
    definition => sub {
        warn "---\n",
            "sequence = ",     $_->att('sequence'), "\n",
            "text = ",         $_->first_child_trimmed_text('text'), "\n",
            "headword = ",     $_->first_child_trimmed_text('headword'), "\n",
            "partOfSpeech = ", $_->first_child_trimmed_text('partOfSpeech'), "\n";
        $_->purge;
    },
})->parsestring($content);

這也更加有效,因為不必將整個結構都加載到內存中( purge方法是為您purge已處理的數據)。

您可以嘗試WWW :: Wordnik :: API (我是作者。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM