2

我有一个如下所示的 XML 数据:

我想要做的是简单地从以下结构中提取出版年份:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<pre> 
    <PubmedArticle>
        <MedlineCitation Owner="NLM" Status="In-Data-Review">
            <PMID Version="1">23853691</PMID>
            <DateCreated>
                <Year>2013</Year>
                <Month>07</Month>
                <Day>15</Day>
            </DateCreated>
            <Article PubModel="Electronic-Print">
                <Journal>
                    <ISSN IssnType="Electronic">1932-6203</ISSN>
                    <JournalIssue CitedMedium="Internet">
                        <Volume>8</Volume>
                        <Issue>5</Issue>
                        <PubDate>
                            <Year>2013</Year>
                        </PubDate>
...
</pre>

但是为什么我的以下代码Perl无法访问“2013”​​年?

use strict;
use Data::Dumper;
use XML::LibXML 1.70;
my $parser = XML::LibXML->new();

my $xmlfilename = "myfile.xml"; 
# obtained from http://dpaste.com/1307466/plain/

my $doc = $parser->parse_file( $xmlfilename );

foreach my $art ( $doc->findnodes('/PubmedArticle/MedlineCitation/Article/Journal/JournalIssue/PubDate') ) {
        my ($year) = $art->findnodes('./Year');
        print Dumper $year->to_literal ;

 }

正确的方法是什么?

4

1 回答 1

3

您忘记了pre根元素。

改变

/PubmedArticle/MedlineCitation/Article/Journal/JournalIssue/PubDate

/pre/PubmedArticle/MedlineCitation/Article/Journal/JournalIssue/PubDate
于 2013-07-17T08:03:14.833 回答