I suspect the line number information is only as good as the underlying StAX parser can provide. The default StAX implementation in the JRE doesn't do a very good job of this, you might want to try Woodstox instead (should just be a case of including an extra JAR or maven dependency).
How to get Apache Axiom to correctly report source data line numbers when parsing XML
Question
Here's what I'm doing, essentially copy/paste:ing from the Axiom Quickstart documentation:
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);
}
}
This works, but the problem is, the line number returned by the element.getLineNumber() always seems to be -1. There's not a lot of documentation on the getLineNumber() method, and it's not easy to find the place where a line number is expected to be defined in the source code. Is there some additional configuration I need to do in order to get Axiom to track line numbers?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow