Hi, iam a newbie.When i use the below code, i was unable to get the
required info from the database. i know there are many errors please
let me know how to rectify them.
import javax.xml.stream.*;
import javax.xml.stream.events.* ;
import java.io.InputStream;
import java.net.URL;
public class EUtilParseExample {
private static final String urlSearch =
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?";
private static final String urlFetch =
"http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?";
public static void main(String[] args) {
InputStream input = null;
try {
URL u = new URL (urlSearch + "db=gene&retmax=10000&term=cmyc");
input = u.openStream();
String filename = "";
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader r = factory.createXMLEventReader(filename, input);
//iterate as long as there are more events on the input stream
while(r.hasNext()) {
XMLEvent e = r.nextEvent();
while (!e.isStartElement() && r.hasNext())
e = r.nextEvent();
if (e.isStartElement() &&
(e.asStartElement().getName().getLocalPart()).equals("Id")) {
getGeneRecord(r.getElementText());
}
}
}
catch (Exception ex) {
System.err.println("Something bad: " + ex);
}
finally {
try {
if (input != null) input.close();
}
catch (Exception ex) {}
}
}
public static void getGeneRecord(String geneid) {
InputStream in = null;
try {
URL u = new URL(urlFetch +"db=gene&rettype=native&termode=xml&id=" +
geneid);
in = u.openStream();
String filename = "";
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader r = factory.createXMLEventReader(filename, in);
//iterate as long as there are more events on the input stream
while(r.hasNext()) {
XMLEvent e = r.nextEvent();
while (!e.isStartElement() && r.hasNext())
e = r.nextEvent();
if (e.isStartElement() &&
(e.asStartElement().getName().getLocalPart()).equals("Gene-ref_desc"))
{
system.out.println(r.getElementText());
}
}
}
catch (Exception ex) {
system.err.println("Something else bad: " + ex);
}
finally {
try {
in.close();
}
catch (Exception ex) {}
}
}
}
Thanks,
L
Oliver Wong - 29 Mar 2006 22:25 GMT
> Hi, iam a newbie.When i use the below code, i was unable to get the
> required info from the database. i know there are many errors please
> let me know how to rectify them.
[code snipped]
What errors are you getting?
- Oliver