尝试使用simplexml_load_file / string访问xml文件

时间:2017-02-25 05:13:58

标签: php xml simplexml

我使用以下内容获取xml文件的内容:

<?php 

parse_str($_SERVER['QUERY_STRING']);

$file_loc="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=".$search.";

$raw_xml = simplexml_load_file($file_loc) or die("Error: Cannot create object");

$xml = simplexml_load_string('<xml_container>'.$raw_xml.'</xml_container>');

$xml = json_decode( json_encode($xml) , 1);

echo "<br> print_r:";
print_r($xml);

echo "<br> vardump:";
var_dump($xml);


?>

使用'ACTN3'作为搜索词的示例,即

https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ACTN3

应该返回:

<eSearchResult>
<Count>248</Count>
<RetMax>20</RetMax>
<RetStart>0</RetStart>
<IdList>
<Id>28195972</Id>
<Id>28177749</Id>
<Id>28177711</Id>
<Id>28154975</Id>
<Id>28139640</Id>
<Id>27966742</Id>
<Id>27913923</Id>
<Id>27861536</Id>
<Id>27821924</Id>
<Id>27819725</Id>
<Id>27798356</Id>
<Id>27747845</Id>
<Id>27601773</Id>
<Id>27584214</Id>
<Id>27508148</Id>
<Id>27442335</Id>
<Id>27361258</Id>
<Id>27294501</Id>
<Id>27274666</Id>
<Id>27188902</Id>
</IdList>
<TranslationSet/>
<TranslationStack>
<TermSet>
<Term>ACTN3[All Fields]</Term>
<Field>All Fields</Field>
<Count>248</Count>
<Explode>N</Explode>
</TermSet>
<OP>GROUP</OP>
</TranslationStack>
<QueryTranslation>ACTN3[All Fields]</QueryTranslation>
</eSearchResult>

而是我的代码返回

print_r:Array ( ) 
vardump:array(0) { }

如何检索XML(例如从https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ACTN3)并打印出来?

1 个答案:

答案 0 :(得分:1)

是的,它返回xml,但问题是你要将字符串追加到object.Try下面:

$file_loc="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=ACTN3";

$raw_xml = simplexml_load_file($file_loc) or die("Error: Cannot create object");
echo "<pre>";
print_r($raw_xml);