Re: SAX succeeds, but StAX fails
I still have the same problem with StAX. I dumped the output of the
url before parsing it, and it seems to be fine and well formed.
But parsing with StAX still gives me an exception right in the first
loop (SAX seems to work fine).
Below is a small test class. Can someone explain to me, why this
happens?
I also tried to copy the output of the url in a file and parsing it
directly from disk ... didn't solve that problem.
Perhaps I should try it with another StAX provider. I found one on the
net named Woodstox. Are there any more? What is the default
implementation? An Apache project?
The error output of the below test class:
START_DOCUMENT: 1.0
beforeNext
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[50,39]
Message: A '(' character or an element type is required in the
declaration of element type "PubMedPubDate".
at
com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:
588)
at StaxTester.main(StaxTester.java:49)
The test class:
import java.net.URL;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class StaxTester {
public static void main(String[] args) {
try {
String address = "http://www.ncbi.nlm.nih.gov/entrez/eutils/
efetch.fcgi?db=pubmed&retmode=xml&id=11748933";
//String address = "http://www.ncbi.nlm.nih.gov/entrez/eutils/
esearch.fcgi?db=pmc&term=stem+cells+AND+free+fulltext[filter]";
URL url = new URL(address);
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader parser =
factory.createXMLStreamReader(url.openConnection().getInputStream());
while(parser.hasNext()) {
switch(parser.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
System.out.println( "START_DOCUMENT: " +
parser.getVersion() );
break;
case XMLStreamConstants.END_DOCUMENT:
System.out.println( "END_DOCUMENT: " );
parser.close();
break;
case XMLStreamConstants.NAMESPACE:
System.out.println( "NAMESPACE: " +
parser.getNamespaceURI() );
break;
case XMLStreamConstants.START_ELEMENT:
System.out.println( "START_ELEMENT: " +
parser.getLocalName() );
break;
case XMLStreamConstants.CHARACTERS:
if ( ! parser.isWhiteSpace() )
System.out.println( "CHARACTERS: " + parser.getText() );
break;
case XMLStreamConstants.END_ELEMENT:
System.out.println("END_ELEMENT: " +
parser.getLocalName() );
break;
default:
break;
}
System.out.println("beforeNext");
parser.next();
System.out.println("afterNext");
}
/** SAX succeeds. Why that? */
// SAXParserFactory parserFactory = SAXParserFactory.newInstance();
// parserFactory.setValidating(true);
// parserFactory.setNamespaceAware(true);
// SAXParser parser = parserFactory.newSAXParser();
// parser.parse(url.openConnection().getInputStream(), new
PubmedEFetchHandler());
//
}
catch (Exception e) {
e.printStackTrace();
}
}
}