Вы можете добавить атрибут с именем _lineNum для каждого элемента, который затем можно использовать.
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.ext.Attributes2Impl;
import javax.xml.parsers.ParserConfigurationException;
class MySlurper extends XmlSlurper {
public static final String LINE_NUM_ATTR = "_srmLineNum"
Locator locator
public MySlurper() throws ParserConfigurationException, SAXException {
super();
}
@Override
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
Attributes2Impl newAttrs = new Attributes2Impl(attrs);
newAttrs.addAttribute(uri, LINE_NUM_ATTR, LINE_NUM_ATTR, "ENTITY", "" + locator.getLineNumber());
super.startElement(uri, localName, qName, newAttrs);
}
}
def text = '''
<root>
<a>one!</a>
<a>two!</a>
</root>'''
def root = new MySlurper().parseText(text)
root.a.each { println it.@_srmLineNum }
Выше добавлен атрибут строки num.Возможно, вы можете попытаться установить свой собственный обработчик ошибок, который может читать номер строки из локатора.