Search code examples
phpxmlnodesixmldomelement

How to get child nodes from an xml url?


I got this link https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text. I am trying to write a code that gets Interactions and GeneOntology within Gene-commentary_heading from the link. I only succeed using this code when there are the 2 or 3 nodes but in this case there are at least 6 nodes or more. Could someone help me?

Bellow is the example of the information I am looking for (it's to much to visualise so I just showed a part)

<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');

}

Solution

  • My answer

    $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++; 
    }