How to get the value of an attribute in an XML node?



Hello,

This is the url
http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmax=10000&term=cmyc
of an xml file that I am trying to read and get the values of the
<IdList> node
in the
- <IdList>
<Id>30686</Id>
<Id>4609</Id>
<Id>17869</Id>
<Id>5728</Id>
<Id>6598</Id>
<Id>9111</Id>
</IdList>

Here is the program that I am trying to read the values of the Id in
the XML file

******************************program****************************
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();
System.out.println(u.toString());
XMLInputFactory factory = null;
System.setProperty("javax.xml.stream.XMLInputFactory",
"com.bea.xml.stream.MXParserFactory");
String filename = "";
//XMLInputFactory factory = XMLInputFactory.newInstance();
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();
System.out.println("The value of e is : " +
e.asStartElement().getName());-- debugging
System.out.println("The value of e is : " +
e.asStartElement().toString());--debugging
if (e.isStartElement() &&
(e.asStartElement().getName().getLocalPart()).equals("Id")) {
System.out.println("The value of e is : " +
e.asStartElement().getName());
//System.out.println("line 4"); -debugging purpose
//System.out.println("The parameter");-- debugging purpose
getGeneRecord(r.getElementText());
//getGeneRecord("30686"); just hard coded one of the Id values just to
see if it works.



}
}
}


catch (Exception ex) {
System.err.println("Something bad: " + ex);
ex.printStackTrace();

}


finally {
try {
if (input != null) input.close();

}


catch (Exception ex) {}

}
}


public static void getGeneRecord(String geneid) {

System.out.println("line 5" + geneid);
InputStream in = null;

try {

System.out.println("line 6");
URL u = new URL(urlFetch +"db=gene&type=native&termode=xml&id=" +
geneid);
in = u.openStream();
System.setProperty("javax.xml.stream.XMLInputFactory",
"com.bea.xml.stream.MXParserFactory");
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();
System.out.println("The value of e is : " +
e.asStartElement().getName());
System.out.println("The value of e is : " +
e.asStartElement().toString());
if (e.isStartElement() &&
//(e.asStartElement().getName().getLocalPart()).equals("Gene-ref_desc"))

(e.asStartElement().getName().getLocalPart()).equals("comment"))
{
System.out.println(r.getElementText());

}
}
}


catch (Exception ex) {
System.err.println("Something else bad: " + ex);
ex.printStackTrace();

}


finally {
try {
in.close();

}


catch (Exception ex) {}

}
}
*****************************End of the
program***********************************************

the program is failing at the line of code r.getElementText());

the error i am getting is
******************Error**************************************
Something bad: javax.xml.stream.XMLStreamException: Precondition for
readText is nextEvent().getTypeEventType() == START_ELEMENT
javax.xml.stream.XMLStreamException: Precondition for readText is
nextEvent().getTypeEventType() == START_ELEMENT
at
com.bea.xml.stream.XMLEventReaderBase.getElementText(XMLEventReaderBase.java:75)
at EUtilParseExample.main(EUtilParseExample.java:44)

****************End of the Error************************


Can some one please help me how do I get the values of the node Id as I
have mentioned in the top?

Thanks
-L

.



Relevant Pages

  • How to get the info from the database
    ... XMLEventReader r = factory.createXMLEventReader; ... catch (Exception ex) ... public static void getGeneRecord(String geneid) { ...
    (comp.lang.java.databases)
  • How to get the info from the database
    ... XMLEventReader r = factory.createXMLEventReader; ... catch (Exception ex) ... public static void getGeneRecord(String geneid) { ...
    (comp.lang.java.gui)
  • Re: Handling Whitespace in Java DOM
    ... another way to handle whitespace in an XML file. ... public static void mainthrows Exception { ...
    (comp.lang.java.programmer)
  • Re: Sending email
    ... I'm even newer to programming email ... using whatever arguments it needs -- uses sendEmail ... line throws an exception, your mailmenssage will never be disposed of. ... public static void forgotPassword ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is It Possible to Make Java Find a File on Your Computer?
    ... The fact that Desktop respects local behavior associations makes it most ... public static void mainthrows Exception { ... public class FileAssociation { ...
    (comp.lang.java.programmer)