Woodstox stax 和 java stax 冲突

发布于 2024-09-29 05:19:34 字数 1339 浏览 3 评论 0原文

我面临着 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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨与橙与城 2024-10-06 05:19:34

好吧,从表面上看,您有两种可能的选择:

  1. 使用依赖项排除来关闭 Woodstox。 CXF 与内置 StaX 一起工作——在内置 Stax 中产生或消除各种错误。

  2. 自己使用 Woodstox。

然而,这里的具体错误有点不太可能。我主要建议将其发布到 CXF 用户列表,并告诉我们您正在使用的 CXF 版本。

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.

述情 2024-10-06 05:19:34

查看异常,它基本上表示无法将 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?

翻了热茶 2024-10-06 05:19:34

此处不应缺少 XMLEvent.START_ELEMENT 案例末尾的 break,否则它将继续到第一个 END_ELEMENT 案例START_ELEMENT 事件,因此出现 ClassCastException。

问题中省略了这部分代码,所以我想我会将其放在这里,以防这个简单的错误可能被忽略。当我意识到我需要休息一下时,这就是我遇到同样异常的原因,无论如何,这导致我来到这里;)

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文