问题
I'm parsing the following String with a StAX XMLEventReader:
final String xmlstr = "<context><book><author>TheName</author></book></context>";
I'm observing the event.getLocation().getCharacterOffset()
value and get some weird behavior: While the index for START_ELEMENT is reported at the position after the element declaration (e.g. index 9 for the context element), the CHARACTERS event for "TheName" is reported to be on index 32. Why? Is there a way to correct that?
回答1:
I encountered a similar problem when going from the latest jdk6 to the latest jdk7. There seems to be a bug in com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl. getCharacterOffset() returns incorrect value suggests using a different XML parser.
(moved from comment to answer)
EDIT: I did some more systematic testing on this, and have found cases in jdk6 that are wrong and in jdk7 that are right and also vice versa. Interestingly, the line number and line column values appear to be consistent and correct across both jdk's.
来源:https://stackoverflow.com/questions/15007961/java-xmlevent-location-characters