douxie2029 2017-03-30 05:27
浏览 48
已采纳

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

Maybe its a question answered before but im so noobie in Web Development. Im trying to get a full XML text from this page:

Human Genome

And, I need to do some XPath queries in that code, like "get the ID" and others. For example:

//eSearchResult/IdList/Id/node()

How I can to get the full XML in a php object to request data throught XPath queries?

I used this code before:

<?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."
";
echo "Name: ".$geneName."
";
echo "Description: ".$geneDesc."
";
echo "Chromosome: ".$geneChromosome."
";?>

But, according with the profesor, this code doesn't use Xpath queries and is required that the page use it.

Someone can help me or explain me how to do it?

  • 写回答

1条回答 默认 最新

  • duanbin198788 2017-06-20 12:33
    关注

    Here's converted code to Xpath query.

    <?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]."
    ";
    
    //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."
    ";
            echo "Description: ".$entry->getElementsByTagName('Description')->item(0)->nodeValue."
    ";
            echo "Chromosome: ".$entry->getElementsByTagName('Chromosome')->item(0)->nodeValue."
    ";
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试