Pubmed fetch xml entry by ID

It wasn’t clear how to grab a pubmed entry in xml format given an ID. This is how to do it:

import scala.io.Source.{fromFile, fromInputStream}
import scala.xml._
import java.net._
import java.io._

val idFile = "medline_ids.txt"

val ids = fromFile( idFile ).getLines()

for ( i <- 0 until 1 ) {
  val url = new URL("http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=" + ids.next + "&retmode=xml")
  println( url )
  val inStream = fromInputStream(url.openStream).getLines.mkString("\n")
  println( inStream ) 


The important part is this:


rather than http://www.ncbi.nlm.nih.gov/pubmed/20980263?report=xml which simply forces the output to look like xml. If you fetch the report=xml code using scala/curl you will see that all the are represented by their html codes – not nice.