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 怎么实现只排序每行中的第二列的数 如果比两数都大就放在第三列中 比两数都小就放在第一列中(语言-c语言)
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4
  • ¥20 MATLAB间接平差计算
  • ¥50 nw.js调用activex
  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 MAC虚拟机(win11)USB插上后无串口com,无法烧录
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到