1
\$\begingroup\$

I have written a code to retrieve data from xml file. It works but it is really slow. I takes about a minute. This is ok for 1 or some more xml files. But If you have to do it for 1000 it's not a good solution. Is there some way that my code could be adapted or improved to make it faster.

Function

Function that retrieve the information for a given path in string format

function xml_retriever($xml_link,$path){
    $result = file_get_contents($xml_link);
    $xml = simplexml_load_string($result);
    $doc = new DOMDocument();
    $doc = DOMDocument::loadXML($xml);
    $xpath = new DOMXPath($doc);
    $entries = $xpath->query($path);
    $attr = '';
    foreach($entries as $node){
        $attr .= '|'.' '.$node->nodeValue. "\r\n";
        $attr = ltrim($attr, '|');
    }
    return $attr;
}

Function test

Simple test if the function works

# Example query and example path
$esearch_test = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text";
$query = "/Entrezgene/Entrezgene_properties/Gene-commentary[3]/Gene-commentary_comment/Gene-commentary[1]/Gene-commentary_comment/Gene-commentary[*]/Gene-commentary_source/Other-source/Other-source_anchor";
# Print the result
echo xml_retriever($esearch_test,$query);

Retrieve all information for every path

Retrieves all information for every term at once.

$esearch_test = "https://www.ncbi.nlm.nih.gov/gene/7128?report=xml&format=text";
$data = array();
foreach ($querys as $key => $value){
    #echo "Key: $key<br /> Value: $value<br />\n";
    echo "<h4>$key: </h4>". xml_retriever($esearch_test,$value)."<br />\n";
    $data[] = xml_retriever($esearch_test,$value);
}

Exception

Function doesn't work for the gene synonyms.

$result = file_get_contents($esearch_test);
$xml = simplexml_load_string($result);
$doc = new DOMDocument();
$doc = DOMDocument::loadXML($xml);

# Add gene synoniem 
$GSyn = "";
foreach($doc->getElementsByTagName('Gene-ref_syn_E') as $node) {
    echo "<pre>".$node->nodeValue."</pre>"; 
    $GSyn .= '|'.' '.$node->nodeValue. "\r\n";
    $GSyn = ltrim($GSyn, '|');
} 
# Add data to the array
array_push($data, $GSyn);
# Print result
print_r($data);
\$\endgroup\$
  • \$\begingroup\$ The site standard is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles. \$\endgroup\$ – BCdotWEB May 18 '18 at 14:16
  • \$\begingroup\$ You say the files are "big". If that means 100Gb, then a minute is perfectly reasonable. If it means 10Mb, then it isn't. Give us some numbers! \$\endgroup\$ – Michael Kay May 18 '18 at 22:34
  • \$\begingroup\$ @nemo so is the string returned by xml_retriever() supposed to have each nodeValue in a string separated by the pipe operator and a new line operator? Also, what doesn't work when running the code for the gene synonyms? I tried it and see all 4 values... \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ May 18 '18 at 22:35
  • \$\begingroup\$ Yes, I want to retrieve a string separated by the pipe operator for later use. Gene synonyms doesn't work when I tried to retrieved using DOMPath method. \$\endgroup\$ – Nemo May 21 '18 at 15:51
  • \$\begingroup\$ The size of my xml files are around 1 till 2MB. Is the cause the performers my computer or the script? \$\endgroup\$ – Nemo May 22 '18 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.