php simplexml_load_file无法获得正确的值

时间:2014-03-31 04:15:42

标签: php

我想从http://www.ebi.ac.uk/ena/data/view/ERS032727&display=xml

获得一些价值
  <?xml version="1.0" encoding="UTF-8"?>
 <ROOT request="ERS032727&amp;display=xml">
 <SAMPLE alias="sample_Ae372-1" center_name="Beijing Genome Institute" accession="ERS032727">
 <IDENTIFIERS>
      <PRIMARY_ID>ERS032727</PRIMARY_ID>
      <EXTERNAL_ID namespace="BioSample">SAMEA762107</EXTERNAL_ID>
      <SUBMITTER_ID namespace="BGI">sample_Ae372-1</SUBMITTER_ID>
 </IDENTIFIERS>
 <TITLE>Acromyrmex echinatior</TITLE>
 <SAMPLE_NAME>
      <TAXON_ID>103372</TAXON_ID>
      <SCIENTIFIC_NAME>Acromyrmex echinatior</SCIENTIFIC_NAME>
      <COMMON_NAME>Panamanian leafcutter ant</COMMON_NAME>
 </SAMPLE_NAME>
 <DESCRIPTION>Genome DNA sample for 500bp and 800bp insert size library</DESCRIPTION>
 <SAMPLE_LINKS>
      <SAMPLE_LINK>
           <XREF_LINK>
                <DB>ENA-STUDY</DB>
                <ID>ERP000666</ID>
           </XREF_LINK>
      </SAMPLE_LINK>
      <SAMPLE_LINK>
           <XREF_LINK>
                <DB>ENA-EXPERIMENT</DB>
                <ID>ERX012712-ERX012713</ID>
           </XREF_LINK>
      </SAMPLE_LINK>
      <SAMPLE_LINK>
           <XREF_LINK>
                <DB>ENA-RUN</DB>
                <ID>ERR034186-ERR034187</ID>
           </XREF_LINK>
      </SAMPLE_LINK>
      <SAMPLE_LINK>
           <XREF_LINK>
                <DB>ENA-SUBMISSION</DB>
                <ID>ERA030423</ID>
           </XREF_LINK>
      </SAMPLE_LINK>
      <SAMPLE_LINK>
           <XREF_LINK>
                <DB>ENA-FASTQ-FILES</DB>
                <ID>http://www.ebi.ac.uk/ena/data/view/reports/sra/fastq_files/ERS032727</ID>
           </XREF_LINK>
      </SAMPLE_LINK>
      <SAMPLE_LINK>
           <XREF_LINK>
                <DB>ENA-SUBMITTED-FILES</DB>
                <ID>http://www.ebi.ac.uk/ena/data/view/reports/sra/submitted_files/ERS032727</ID>
           </XREF_LINK>
      </SAMPLE_LINK>
 </SAMPLE_LINKS>
 <SAMPLE_ATTRIBUTES>
      <SAMPLE_ATTRIBUTE>
           <TAG>Time of sample collection</TAG>
           <VALUE>2008</VALUE>
           <UNITS>Year</UNITS>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>Habitat</TAG>
           <VALUE>Gamboa, Panama</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>Sample extracted from</TAG>
           <VALUE>1 male</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>Sample type</TAG>
           <VALUE>DNA</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>ENA-SPOT-COUNT</TAG>
           <VALUE>119159746</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>ENA-BASE-COUNT</TAG>
           <VALUE>23831949200</VALUE>
      </SAMPLE_ATTRIBUTE>
 </SAMPLE_ATTRIBUTES>

    

我使用$ file = simplexml_load_file($ path),但我只是想得到这个部分

<SAMPLE_ATTRIBUTES>
      <SAMPLE_ATTRIBUTE>
           <TAG>Time of sample collection</TAG>
           <VALUE>2008</VALUE>
           <UNITS>Year</UNITS>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>Habitat</TAG>
           <VALUE>Gamboa, Panama</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>Sample extracted from</TAG>
           <VALUE>1 male</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>Sample type</TAG>
           <VALUE>DNA</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>ENA-SPOT-COUNT</TAG>
           <VALUE>119159746</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
           <TAG>ENA-BASE-COUNT</TAG>
           <VALUE>23831949200</VALUE>
      </SAMPLE_ATTRIBUTE>
 </SAMPLE_ATTRIBUTES>

我设置$ file-&gt; SAMPLE_ATTRIBUTES,但无法正常工作。 如何获取样本属性中的每个样本属性 喜欢

Time of sample collection= 2008 Year
Habitat= Gamboa, Panama
Sample extracted from= 1 male
....
.....

2 个答案:

答案 0 :(得分:0)

使用simplexml_load_stringforeach进行循环播放。

$xml = simplexml_load_string(file_get_contents('http://www.ebi.ac.uk/ena/data/view/ERS032727&display=xml'));
foreach ($xml->SAMPLE->SAMPLE_ATTRIBUTES->SAMPLE_ATTRIBUTE as $child)
{
    echo $child->TAG." = ".$child->VALUE;
    echo "<br>";
}

<强> OUTPUT :

Time of sample collection = 2008
Habitat = Gamboa, Panama
Sample extracted from = 1 male
Sample type = DNA
ENA-SPOT-COUNT = 119159746
ENA-BASE-COUNT = 23831949200

答案 1 :(得分:0)

您需要遍历文档到您感兴趣的节点.XPATH表达式将是

/ROOT/SAMPLE/SAMPLE_ATTRIBUTES/SAMPLE_ATTRIBUTE

使用SimpleXMLElementsimplexml_load_*创建的实例代表根节点,因此遍历将是

$file->SAMPLE->SAMPLE_ATTRIBUTES->SAMPLE_ATTRIBUTE

由于这代表一个集合,您可以使用传统方法进行迭代......

foreach ($file->SAMPLE->SAMPLE_ATTRIBUTES->SAMPLE_ATTRIBUTE as $attribute) {
    printf('%s= %s%s<br>',
        $attribute->TAG, $attribute->VALUE,
        isset($attribute->UNITS) ? " {$attribute->UNITS}" : '');
}