Woodstox stax 和 java stax 冲突
我面临着 Woodstox STAX 和 java 1.6 STAX 实现之间奇怪的运行时冲突。由于我使用的是 CXF,它会将 Woodstox jar 作为其依赖项的一部分。这是我正在使用的示例代码。
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
在运行时,我收到以下异常。
java.lang.Exception: java.lang.ClassCastException: com.ctc.wstx.evt.CompactStartElement cannot be cast to javax.xml.stream.events.EndElement
当它到达行 EndElement endElement = event.asEndElement();
我有点困惑为什么它在这一点上引起,尽管它没有失败 StartElement se = event.asStartElement();
在调试时,我发现事件对象是 com.ctc.wstx.evt 包的一部分,而不是 javax.xml.stream。但不知道为什么它之前没有失败。
任何指针都将受到高度赞赏。
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 inStartElement 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,从表面上看,您有两种可能的选择:
使用依赖项排除来关闭 Woodstox。 CXF 与内置 StaX 一起工作——在内置 Stax 中产生或消除各种错误。
自己使用 Woodstox。
然而,这里的具体错误有点不太可能。我主要建议将其发布到 CXF 用户列表,并告诉我们您正在使用的 CXF 版本。
Well, you have two possible choices from a superficial view:
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.
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.
查看异常,它基本上表示无法将 StartElement 转换为 EndElement;这似乎并不是 stax 实现之间的不兼容,而是某个地方的错误。这是 Woodstox 的哪个版本?
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?
此处不应缺少
XMLEvent.START_ELEMENT
案例末尾的break
,否则它将继续到第一个END_ELEMENT
案例START_ELEMENT 事件,因此出现 ClassCastException。问题中省略了这部分代码,所以我想我会将其放在这里,以防这个简单的错误可能被忽略。当我意识到我需要休息一下时,这就是我遇到同样异常的原因,无论如何,这导致我来到这里;)
A
break
at the end ofXMLEvent.START_ELEMENT
case should not be missing here otherwise it will just going to continue to theEND_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 ;)