[jdom-interest] How to find out the line number in the XML document instance

Laurent Bihanic laurent.bihanic at atosorigin.com
Fri Apr 12 04:48:47 PDT 2002

Hi Olle,

JDOM does not currently store the line number information. But there are some 
plugs that should allow you to access it.

First you'll need too write your own subclass of the JDOM node classes 
(Element, Comment PI, etc.) for which you wish to keep the line number 
information. To allow SAXBuilder to allocate instances of these classes, 
you'll need to subclass DefaultJDOMFactory to get the line number information 
and store it in your node instances.

How does the factory access the line number information? It can use the 
getDocumentLocator method provided by SAXHandler. But... to access the 
SAXHandler instance you'll need to create your own subclass of SAXBuilder.

It's not as complicated as it seems:

public class MyBuilder extends SAXBuilder {
   // Copy constructors here...

   protected void configureContentHandler(SAXHandler contentHandler) {

public class MyFactory extends DefaultJDOMFactory {
   private SAXHandler saxHandler;


   public void setSAXHandler(SAXHandler sh) {
      this.saxHandler = sh;

   public Element element(String name) {
      this.element(name, (Namespace) null);

   public Element element(String name, Namespace namespace) {
      MyElement e = new MyElement(name, namespace);

      org.xml.sax.Locator loc = this.saxHandler.getDocumentLocator()

Hope this helps,


Olle Hallin wrote:
> Hi all JDOM gurus!
> I am writing a product configurator driven by an XML document that defines all products and their components.
> In order to keep it simple, the plan is to let the product managers maintain this file directly by means of XML Spy assisted by an XML Schema.
> Many errors will be trapped by the schema, but not all.
> My question now is how to find the line number in the original XML document instance that corresponds to a certain JDOM Element?
> I want to give as exakt error messages as possible should I detect an error in an XML instance that has passed the schema validation.
>  I use Xerces-J 2.0.0 as parser.
> Regards,
> Olle Hallin
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

                  wWw    Zzzzz
                 (- -)
Laurent Bihanic           | Tel: +33 (0)1  (Direct line)
Atos Origin               |      +33 (0)1
Intégration - e-Business  | Fax: +33 (0)1
Les Miroirs - Bat. C      |
18, avenue d'Alsace       |
F-92926 La Defense Cedex  | e-Mail: laurent.bihanic at atosorigin.com

"Microsoft isn't the answer. Microsoft is the question and the answer
  is no."

The opinions expressed are entirely my own and may not necessarily be
those of my employer.  Also, I am not now nor have I ever been a
lawyer.  My opinions are provided as-is with absolutely no warrantee of
merchantability or fitness for any particular use.  Besides, you can't
prove I typed this.  No body saw me type this.  Who says I typed this?

More information about the jdom-interest mailing list