Get line number from xml node - java

前端 未结 4 577
逝去的感伤
逝去的感伤 2020-12-01 15:43

I have parsed an XML file and have gotten a Node that I am interested in. How can I now find the line number in the source XML file where this node occurs?

EDIT: Cur

4条回答
  •  [愿得一人]
    2020-12-01 16:09

    If you are using a SAX parser then the line number of an event can be obtained using the Locator object, which is notified to the ContentHandler via the setDocumentLocator() callback. This is called at the start of parsing, and you need to save the Locator; then after any event (such as startElement()), you can call methods such as getLineNumber() to obtain the current position in the source file. (After startElement(), the callback is defined to give you the line number on which the ">" of the start tag appears.)

提交回复
热议问题