以下是我正在做的事情,主要是复制/粘贴:来自Axiom快速入门文档:
public GenericXmlParser(Reader input, String entryElementName, List<DciXmlMapping> dciXmlMappings) {
this.input = OMXMLBuilderFactory.createOMBuilder(input).getDocument().getXMLStreamReader(false);
this.dciXmlMappings = dciXmlMappings;
this.entryElementName = new QName(entryElementName);
}
public FeedEntry next() {
try {
while (input.hasNext()) {
if (input.getEventType() == XMLStreamReader.START_ELEMENT &&
input.getName().equals(entryElementName)) {
OMElement element =
OMXMLBuilderFactory.createStAXOMBuilder(input).getDocumentElement();
// Make sure that all events belonging to the element are consumed so that
// that the XMLStreamReader points to a well defined location (namely the
// event immediately following the END_ELEMENT event).
element.build();
// Now process the element.
return new FeedEntry(element.getLineNumber(), processEntryElement(element), ImmutableList.<String>of());
}
input.next();
}
return null;
}
catch (XMLStreamException e) {
throw new ImpException(e);
}
}
这样可行,但问题是,element.getLineNumber()返回的行号似乎总是为-1。关于getLineNumber()方法的文档并不多,而且找不到源代码中预定行号的地方也不容易。为了让Axiom跟踪行号,我需要做一些额外的配置吗?
答案 0 :(得分:0)
我怀疑行号信息只能与底层StAX解析器提供的信息一样好。 JRE中的默认StAX实现对此没有很好的作用,您可能希望尝试Woodstox(应该只是包含额外的JAR或maven依赖项)。