Как получить дочерние узлы из XML-URL? - PullRequest
0 голосов
/ 02 мая 2018

Я получил эту ссылку https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text. Я пытаюсь написать код, который получает взаимодействия и GeneOntology в Gene-commentary_heading по ссылке. Я успешно использую этот код только тогда, когда есть 2 или 3 узла, но в этом случае есть по крайней мере 6 узлов или больше. Может ли кто-нибудь помочь мне?

Ниже приведен пример информации, которую я ищу (это много для визуализации, поэтому я только что показал часть)

<Gene-commentary_heading>GeneOntology</Gene-commentary_heading>
  <Gene-commentary_source>
    <Other-source>
      <Other-source_pre-text>Provided by</Other-source_pre-text>
      <Other-source_anchor>GOA</Other-source_anchor>
      <Other-source_url>http://www.ebi.ac.uk/GOA/</Other-source_url>
    </Other-source>
  </Gene-commentary_source>
  <Gene-commentary_comment>
    <Gene-commentary>
      <Gene-commentary_type value="comment">254</Gene-commentary_type>
      <Gene-commentary_label>Function</Gene-commentary_label>
      <Gene-commentary_comment>
        <Gene-commentary>
          <Gene-commentary_type value="comment">254</Gene-commentary_type>
          <Gene-commentary_source>
            <Other-source>
              <Other-source_src>
                <Dbtag>
                  <Dbtag_db>GO</Dbtag_db>
                  <Dbtag_tag>
                    <Object-id>
                      <Object-id_id>3677</Object-id_id>
                    </Object-id>
                  </Dbtag_tag>
                  ...



`$url = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text";


$document_xml = new DOMDocument();

$document_xml->loadXML($url);

$elements = $url->getElementsByTagName('Gene-commentary_heading');

echo $elements;

foreach($element as $node) {

    $GO = $node -> getElementsByTagName('GeneOntology');

    $Int = $node->getElementsByTagName('Interactions');

}

1 Ответ

0 голосов
/ 09 мая 2018

Мой ответ

$esearch_test = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text";
$result = file_get_contents($esearch_test);
$xml = simplexml_load_string($result);
$doc = new DOMDocument();
$doc = DOMDocument::loadXML($xml);
$c = 1;
foreach($doc->getElementsByTagName('Gene-commentary_heading') as $node) {
echo "$c: ".$node->textContent."\n";
$c++; 
}
...