使用php获取网页XML代码并在其上使用XPATH

使用php获取网页XML代码并在其上使用XPATH,php,xml,xpath,Php,Xml,Xpath,也许这是一个以前被回答过的问题,但我在网络开发方面是如此无所事事。 我正在尝试从此页面获取完整的XML文本: 而且,我需要在代码中执行一些XPath查询,比如“获取ID”等。 例如: //eSearchResult/IdList/Id/node() 如何通过XPath查询获取php对象中的完整XML以请求数据 我以前使用过此代码: <?php $text = $_REQUEST['text']; $xmlId = simplexml_load_file('https://eutils.

也许这是一个以前被回答过的问题,但我在网络开发方面是如此无所事事。 我正在尝试从此页面获取完整的XML文本:

而且,我需要在代码中执行一些XPath查询,比如“获取ID”等。 例如:

//eSearchResult/IdList/Id/node()
如何通过XPath查询获取php对象中的完整XML以请求数据

我以前使用过此代码:

<?php
$text = $_REQUEST['text'];
$xmlId = simplexml_load_file('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;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&amp;id='.$id.'&amp;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";?>
IdList[0]->Id;
$xmlGeneralData=simplexml\u加载\u文件('https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gene&;id='.$id.&;retmode=xml');
$geneName=$xmlGeneralData->DocumentSummarySet->DocumentSummary[0]->名称;
$geneDesc=$xmlGeneralData->DocumentSummarySet->DocumentSummary[0]->说明;
$GeneComponse=$xmlGeneralData->DocumentSummarySet->DocumentSummary[0]->染色体;
回显“Id:”.$Id.“\n”;
echo“Name:.$geneName.\n”;
echo“Description:.$geneDesc.\n”;
echo“染色体:.$GeneComponse.\n”?>
但是,根据profesor的说法,这段代码不使用Xpath查询,页面必须使用它


有人可以帮助我或向我解释怎么做?

这里是转换成Xpath查询的代码

<?php

$text = $_REQUEST['text'];
$xmlId = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;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&amp;id='.$id[0].'&amp;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";
    }
}

?>
load($xmlId);
$xpath=newdomxpath($xml\u id);
//Xpath查询以获取ID
$elements=$xpath->query(“//eSearchResult/IdList/Id”);
//循环遍历xpath查询的结果并存储在ID数组中
如果($elements->length>0){
foreach($elements作为$entry){
$id[]=$entry->nodeValue;
}
}
回显“Id:”.$Id[0]。“\n”;
//从xpath结果集中输出ID数组的第一个字符串
$xmlGeneralData='0https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=gene&id='.$id[0].&;retmode=xml';
//加载XML并定义Xpath
$xml_gd=new DOMDocument();
$xml_gd->load($xmlGeneralData);
$xpath=newdomxpath($xml\u-gd);
//Xpath查询,用于使用上一个结果集中ID数组的第一个字符串搜索文档摘要
$elements=$xpath->query(//eSummaryResult/DocumentSummarySet/DocumentSummary[@uid='“$id[0]。”]);
//循环遍历xpath查询的结果,查找节点并打印结果
如果($elements->length>0){
foreach($elements作为$entry){
echo“Name:”.$entry->getElementsByTagName('Name')->item(0)->nodeValue。“\n”;
echo“Description:”.$entry->getElementsByTagName('Description')->item(0)->nodeValue。“\n”;
echo“chromose:”.$entry->getElementsByTagName('chromose')->项(0)->节点值。“\n”;
}
}
?>