这就是我正在做的事情,基本上是从 Axiom Quickstart 文档中复制/粘贴:
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 跟踪行号,我需要做一些额外的配置吗?