0

我正在编写一个脚本来输出从 PubMed Central API 抓取的排序信息。我的问题是我想提取第一作者和第二作者以及第一个和第二个关键字(就这个问题而言,我们可以只关注一个)。一个很好的例子是在$xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->surname。显然,我可以得到要打印的名字。

根据我阅读其他帖子的理解,我需要一个foreach循环来实现我想要的。但是,我不明白如何在这种情况下实现这一点。我的代码如下:

<?php
$PMCID = 3545513;
$url = 'http://www.pubmedcentral.nih.gov/oai/oai.cgi?verb=GetRecord&identifier=oai:pubmedcentral.nih.gov:'.$PMCID.'&metadataPrefix=pmc_fm';
$xml = new SimpleXMLElement(file_get_contents($url));
?>
<table>
<tr>
<td>Journal Title</td><td>Year</td><td>Issue</td><td>NOC_Country</td><td>State</td><td>City</td><td>Primary Institution</td><td>Secondary Institution</td><td>First Author</td><td>Second Author</td><td>Topic</td><td>Target Behavior 1</td><td>Target Behavior 2</td><td>Population</td><td>Paper</td><td>Status</td>
</tr>
<tr>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'journal-meta'}->{'journal-title-group'}->{'journal-title'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'pub-date'}->year;?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->issue;?></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->surname;?>, <?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->{'given-names'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->surname;?>, <?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'}->contrib->name->{'given-names'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'title-group'}->{'article-title'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'kwd-group'}->kwd;?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'kwd-group'}->kwd;?></td>
<td></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'article-categories'}->{'subj-group'}->subject;?></td>
<td></td>
</tr>
</table>

任何帮助将不胜感激!

4

1 回答 1

0

结束了:

<table>
<tr>
<td><strong>Journal Title</strong></td><td><strong>Year</strong></td><td><strong>Issue</strong></td><td><strong>First Author</strong></td><td><strong>Second Author</strong></td><td><strong>Topic</strong></td><td><strong>Target Behavior 1</strong></td><td><strong>Target Behavior 2</strong></td><td><strong>Paper</strong></td>
</tr>
<?php
$PMCID = $_REQUEST['PMCID'];
$url = 'http://www.pubmedcentral.nih.gov/oai/oai.cgi?verb=GetRecord&identifier=oai:pubmedcentral.nih.gov:'.$PMCID.'&metadataPrefix=pmc_fm';
$xml = new SimpleXMLElement(file_get_contents($url));
?>
<tr>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'journal-meta'}->{'journal-title-group'}->{'journal-title'};?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'pub-date'}->year;?></td>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->issue;?></td>
<?php
$n=0;
foreach($xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'contrib-group'} as $author){
    echo "<td>" . $author->contrib->name->surname . ", ";
    echo $author->contrib->name->{'given-names'} . "</td>";
$n++;
if($n==2) break;
}
unset($n);
unset($author);
?>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'title-group'}->{'article-title'};?></td>
<?php
$i=0;
foreach($xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'kwd-group'}->kwd as $keyword){
    echo "<td>" . $keyword . "</td>";
$i++;
if($i==2) break;
}
unset($i);
unset($keyword);
?>
<td><?php echo $xml->GetRecord->record->metadata->article->front->{'article-meta'}->{'article-categories'}->{'subj-group'}->subject;?></td>
</tr>
</table>
于 2013-09-12T19:45:18.057 回答