STaX 解析器的行为不同
我使用的是 java 6,rt.jar 具有进行基于 STaX 的解析所需的所有类。我主要使用以下类:
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
当我作为 java 应用程序运行以下内容时:
while(reader.hasNext()) {
XMLEvent elementsEvent = reader.nextEvent();
System.out.println(elementsEvent.toString());
--------
}
它按预期工作,但是当我将其部署到 JBoss 中时,它的行为有所不同。有谁知道这里可能有什么问题?
I am using java 6 and rt.jar has all the classes required to do STaX based parsing. Mainly I am using following classes:
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
When I run following as an java application:
while(reader.hasNext()) {
XMLEvent elementsEvent = reader.nextEvent();
System.out.println(elementsEvent.toString());
--------
}
It works as expected, but when I deploy this into JBoss it behaves differently. Does anyone knows what might be the problem here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JBoss 使用此处的 RI:http://stax.codehaus.org/Home 作为 STAX 提供程序。我猜想 JDK 可能默认使用 SJSXP。
由于
toString()
的行为未在XMLEvent
的 API 上指定,因此打印结果取决于实现。有些实现会转储普通的旧Object#toString()
,有些可能会写出 XML,等等。实际上,您依赖于 javax 包接口未指定的实现细节。您可以通过在开发环境中使用 codehaus stax 实现来获得一致的行为,但当然您仍然使用供应商特定的功能。
(过去,当 Sun 更改提供程序时,其他 XML API 也出现了这个问题。org.w3c.dom.Element 曾经从 toString 打印 XML,但这是提供程序的行为,然后有一天它发生了变化,许多人的代码坏了!)
JBoss uses the RI from here: http://stax.codehaus.org/Home for the STAX provider. I would guess JDK probably uses SJSXP by default.
Since the behavior of
toString()
isn't specified on the API forXMLEvent
, it's up to the implementation what it happens to feel like printing out. Some implementations dump plain oldObject#toString()
, some might write out the XML, etc. Effectively you're relying on implementation details that aren't specified by the javax package interfaces.You can get consistent behavior by using the codehaus stax implementation in your development environment, but of course you are still using vendor specific functionality.
(This problem has creeped up in the past with other XML APIs as well when Sun changed the provider. org.w3c.dom.Element used to print XML from toString, but it was provider behavior, then one day it changed and many people's code broke!)