views:

16

answers:

1

I'm facing a weird runtime conflict between Woodstox STAX and java 1.6 STAX implementation. Since I'm using CXF,its pulling Woodstox jar as part of its dependency. Here's a sample code I'm using.

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

XMLInputFactory factory = (XMLInputFactory)XMLInputFactory.newInstance();
XMLEventReader reader =
factory.createXMLEventReader(new StringReader(xml)); 
while (reader.hasNext()){
XMLEvent event = reader.nextEvent();
switch (event.getEventType()){
case XMLEvent.START_ELEMENT :
StartElement se = event.asStartElement();
...........
...........
case XMLEvent.END_ELEMENT :
EndElement endElement = event.asEndElement();
if (event.asEndElement().getName().getLocalPart()==("document"))
// do something

During runtime, I'm getting the following exception.

java.lang.Exception: java.lang.ClassCastException: com.ctc.wstx.evt.CompactStartElement cannot be cast to javax.xml.stream.events.EndElement

when it reaches line EndElement endElement = event.asEndElement();

I'm sort of puzzled why its causing at this point though it doesn't fail in StartElement se = event.asStartElement();

While debugging, I found that the event objects are part of com.ctc.wstx.evt package and not javax.xml.stream. But not sure why its not failing before.

Any pointer will be highly appreciated.

+1  A: 

Well, you have two possible choices from a superficial view:

  1. Use a dependency exclusion to turn off Woodstox. CXF works with the built-in StaX -- give or take the various bugs in the built-in Stax.

  2. Use Woodstox yourself.

However, the specific error here is a bit unlikely. I mostly recommend posting this to the cxf users list, and telling us there exactly what version of CXF you are using.

bmargulies
Thanks bmargulies .. I was also thinking of using the woodstox objects directy, but was not able to find documentation / example which will provide some info...
Shamik
Just use the factories in the Woodstox package. Their javadoc has them.
bmargulies