Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

131 views
Skip to first unread message

lear...@gmail.com

unread,
Apr 5, 2006, 9:30:48 PM4/5/06
to
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

0 new messages