[jdom-interest] How can I get the line number of an element?

Laurent Bihanic laurent.bihanic at atosorigin.com
Tue Apr 1 03:48:02 PST 2003


Hi,

Rick Horowitz wrote:
> Is there a way to determine the line number of an element and/or
> attribute when reading an XML file using JDOM? If so, how?

The line number information is available through method getLineNumber() of the 
Locator object provided by the SAXParser. SAXHandler provides the public 
method getDocumentLocator() for getting access to this locator.
But the SAXHandler instance is well hidden within SAXBuilder. You'll need to 
subclass SAXBuilder to overwrite the createContentHandler() method to store 
the reference to the SAXHandler instance and add a public method to make it or 
the Locator or the actual line number information available.

Be careful though, the information provided by the locator is only valid while 
the parse is ongoing. Thus the only way, in JDOM, to get meaningful line 
numbers is to retrieve this information from a JDOMFactory, when elements are 
being created.

Hope this helps,

Laurent




More information about the jdom-interest mailing list