Skip to content

Instantly share code, notes, and snippets.

@lindenb
Created April 23, 2019 19:28
Show Gist options
  • Save lindenb/a5a8fa1b8e3c855917235df347f20f44 to your computer and use it in GitHub Desktop.
Save lindenb/a5a8fa1b8e3c855917235df347f20f44 to your computer and use it in GitHub Desktop.
https://www.biostars.org/p/376339/ pubmed xpath xml java unitprot
import java.net.*;
import java.util.*;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
public class Biostar376339 {
public static void main(final String args[]) {
try {
String urlstr="https://www.uniprot.org/uniprot/P0AD86.xml";
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(urlstr);
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();
XPathExpression expr = xpath.compile("//*[local-name()='dbReference' and @type='PubMed']/@id");
XPathExpression expr2 = xpath.compile("/PubmedArticleSet/PubmedArticle/MedlineCitation/Article/ArticleTitle");
NodeList nodes = (NodeList)expr.evaluate(dom, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
final String pmid=nodes.item(i).getNodeValue();
Document dom2 = builder.parse("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?retmode=xml&db=pubmed&id="+pmid);
System.out.println(pmid+" "+expr2.evaluate(dom2, XPathConstants.STRING));
}
}
catch(Exception err) {
err.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment