也许它之前的问题得到了回答,但在网络开发方面我是如此的无聊。 我试图从这个页面获得完整的XML文本:
https://github.com/spring-cloud/spring-cloud-dataflow/issues/915
并且,我需要在该代码中执行一些XPath查询,例如"获取ID"和别的。 例如:
//eSearchResult/IdList/Id/node()
如何在php对象中获取完整的XML以通过XPath查询请求数据?
之前我使用过这段代码:
<?php
$text = $_REQUEST['text'];
$xmlId = simplexml_load_file('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&term='.$text.'%5bGene%20Name%5d+AND+%22Homo%20sapiens%22%5bOrganism');
$id = $xmlId->IdList[0]->Id;
$xmlGeneralData = simplexml_load_file('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gene&id='.$id.'&retmode=xml');
$geneName = $xmlGeneralData->DocumentSummarySet->DocumentSummary[0]->Name;
$geneDesc = $xmlGeneralData->DocumentSummarySet->DocumentSummary[0]->Description;
$geneChromosome = $xmlGeneralData->DocumentSummarySet->DocumentSummary[0]->Chromosome;
echo "Id: ".$id."\n";
echo "Name: ".$geneName."\n";
echo "Description: ".$geneDesc."\n";
echo "Chromosome: ".$geneChromosome."\n";?>
但是,根据profesor的说法,此代码不使用Xpath查询,并且要求页面使用它。
有人可以帮助我或解释我该怎么做?
答案 0 :(得分:0)
Here's converted code to Xpath query.
<?php
$text = $_REQUEST['text'];
$xmlId = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&term='.$text.'%5bGene%20Name%5d+AND+%22Homo%20sapiens%22%5bOrganism';
//Load XML and define Xpath
$xml_id = new DOMDocument();
$xml_id->load($xmlId);
$xpath = new DOMXPath($xml_id);
//Xpath query to get ID
$elements = $xpath->query("//eSearchResult/IdList/Id");
//Loop through result of xpath query and store in array of ID
if ($elements->length >0) {
foreach ($elements as $entry) {
$id[] = $entry->nodeValue;
}
}
echo "Id: ".$id[0]."\n";
//Output the first string of ID array from xpath result set
$xmlGeneralData = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gene&id='.$id[0].'&retmode=xml';
//Load XML and define Xpath
$xml_gd = new DOMDocument();
$xml_gd->load($xmlGeneralData);
$xpath = new DOMXPath($xml_gd);
//Xpath query to search for Document Summary with first string of ID array from previous result set
$elements = $xpath->query("//eSummaryResult/DocumentSummarySet/DocumentSummary[@uid='".$id[0]."']");
//Loop through result of xpath query and find nodes and print out the result
if ($elements->length >0) {
foreach ($elements as $entry) {
echo "Name: ".$entry->getElementsByTagName('Name')->item(0)->nodeValue."\n";
echo "Description: ".$entry->getElementsByTagName('Description')->item(0)->nodeValue."\n";
echo "Chromosome: ".$entry->getElementsByTagName('Chromosome')->item(0)->nodeValue."\n";
}
}
?>