我有一个XML here,其中包含作者列表
...
<contributors>
<person_name contributor_role="author" sequence="first">
<given_name>John</given_name>
<surname>Doe</surname>
</person_name>
<person_name contributor_role="author" sequence="first">
<given_name>Micheal</given_name>
<surname>Bob</surname>
</person_name>
<person_name contributor_role="author" sequence="additional">
<given_name>Lyly</given_name>
<surname>Jane</surname>
</person_name>
<person_name contributor_role="author" sequence="additional">
<given_name>Tom</given_name>
<surname>Mark</surname>
</person_name>
</contributors>
...
我尝试使用下面的代码来获取具有全名(给定名称和姓氏)但工作不正常的所有作者-显示空白值,并用“;”分隔因为我在代码中使用过
$xml2 = simplexml_load_file($api_xml_url);
foreach($xml2 as $contributors =>$node) {
foreach($node as $person_name => $child) {
$authors = $child->given_name ." " .$child->surname ."; ";
echo $authors;
}
如何按约翰·多伊(John Doe)的顺序显示所有作者;米歇尔·鲍勃;莉莉·简汤姆·马克
答案 0 :(得分:2)
根据您在此链接上提供的XML:XML File
这是解决方案:
foreach($xml2->query_result->body->query->doi_record->crossref->journal->journal_article->contributors as $contributors) {
foreach($contributors as $person) {
$author = $person->given_name ." " .$person->surname ."; ";
echo $author;
}
}
经过测试,可以正常工作。