STaX parser behaving differently
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 = reade开发者_运维百科r.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?
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 for XMLEvent
, it's up to the implementation what it happens to feel like printing out. Some implementations dump plain old Object#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!)
精彩评论