开发者

Woodstox stax and java stax conflict

开发者 https://www.devze.com 2023-01-22 04:01 出处:网络
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.

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 p开发者_开发知识库oint 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.


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.


Looking at the exception, it says basically that one can not cast StartElement to EndElement; it does not seem like an incompatibility between stax implementations but rather a bug somewhere. Which Woodstox version is this?


A break at the end of XMLEvent.START_ELEMENT case should not be missing here otherwise it will just going to continue to the END_ELEMENT case with the first START_ELEMENT event, hence the ClassCastException.

That part of the code have been omitted from the question so I thought I would put this here just in case this simple error might have been overlooked. It's how I got the same exception that led me here anyway when I realised I needed a break ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号