Java, XMLEvent location Characters

孤街浪徒 提交于 2019-12-08 01:28:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!