Reporting line number of parse exception?

Discussion in 'XML' started by rolf@squarebox.co.uk, Jun 22, 2005.

  1. Guest

    Hmm, I seem to be missing something basic with regards to SAXParsers.

    I'm parsing some XML using Java 1.4.2 and SAX, so I extend
    DefaultHandler and in my startElement() method I detect some kind of
    error, maybe a missing mandatory attribute. So I throw an exception,
    and of course I want to report the location (line number and character)
    within the XML file that the error occurred in. I could throw a
    SAXParseException that has this information, but... where do I get the
    location from? It seems likke I could do with a Locator, but
    DefaultHandler only has a setLocator(), no getLocator(). Surely the
    parser or the InputSource knows where it's got to in the file when an
    error occurs - is there no way of getting hold of this information?

    I'm constructing the parser as follows:

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    InputStream in = new FileInputStream(filename);
    MyHandler handler = new MyHandler();
    InputSource source = new InputSource(in);
    parser.parse(source, handler);

    -Rolf
     
    , Jun 22, 2005
    #1
    1. Advertising

  2. wrote:

    > Hmm, I seem to be missing something basic with regards to SAXParsers.
    >
    > I'm parsing some XML using Java 1.4.2 and SAX, so I extend
    > DefaultHandler and in my startElement() method I detect some kind of
    > error, maybe a missing mandatory attribute. So I throw an exception,
    > and of course I want to report the location (line number and character)
    > within the XML file that the error occurred in. I could throw a
    > SAXParseException that has this information, but... where do I get the
    > location from? It seems likke I could do with a Locator, but
    > DefaultHandler only has a setLocator(), no getLocator(). Surely the
    > parser or the InputSource knows where it's got to in the file when an
    > error occurs - is there no way of getting hold of this information?
    >
    > I'm constructing the parser as follows:
    >
    > SAXParserFactory factory = SAXParserFactory.newInstance();
    > SAXParser parser = factory.newSAXParser();
    > InputStream in = new FileInputStream(filename);
    > MyHandler handler = new MyHandler();
    > InputSource source = new InputSource(in);
    > parser.parse(source, handler);


    As far as I understand it your handler needs to implement the method
    setDocumentLocator
    and if the SAX parser provides location information then it will call
    that method and you can then process and store the Locator passed in and
    access it when needed to call methods like
    getLineNumber()
    getColumnNumber()


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 22, 2005
    #2
    1. Advertising

  3. Rolf Howarth Guest

    Martin Honnen wrote:
    >
    > > I'm parsing some XML using Java 1.4.2 and SAX, so I extend
    > > DefaultHandler and in my startElement() method I detect some kind of
    > > error, maybe a missing mandatory attribute. So I throw an exception,
    > > and of course I want to report the location (line number and character)
    > > within the XML file that the error occurred in. I could throw a
    > > SAXParseException that has this information, but... where do I get the
    > > location from? It seems likke I could do with a Locator, but
    > > DefaultHandler only has a setLocator(), no getLocator(). Surely the
    > > parser or the InputSource knows where it's got to in the file when an
    > > error occurs - is there no way of getting hold of this information?

    >
    > As far as I understand it your handler needs to implement the method
    > setDocumentLocator
    > and if the SAX parser provides location information then it will call
    > that method and you can then process and store the Locator passed in and
    > access it when needed to call methods like
    > getLineNumber()
    > getColumnNumber()


    Right, but where does my handler get the Locator from to pass to
    setDocumentLocator? javax.xml.parsers.SAXParser doesn't have any
    methods to give me the information I need to implement Locator.

    -Rolf
     
    Rolf Howarth, Jun 22, 2005
    #3
  4. Rolf Howarth wrote:


    >>As far as I understand it your handler needs to implement the method
    >> setDocumentLocator
    >>and if the SAX parser provides location information then it will call
    >>that method and you can then process and store the Locator passed in and
    >>access it when needed to call methods like
    >> getLineNumber()
    >> getColumnNumber()

    >
    >
    > Right, but where does my handler get the Locator from to pass to
    > setDocumentLocator? javax.xml.parsers.SAXParser doesn't have any
    > methods to give me the information I need to implement Locator.


    You need to implement the interface in your handler and the SAX parser
    then calls the method you provide and passes the Locator in.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 23, 2005
    #4
  5. Rolf Howarth Guest

    Thanks Martin, that did the trick.

    -Rolf
     
    Rolf Howarth, Jun 25, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Liang
    Replies:
    4
    Views:
    841
    Liang
    Dec 31, 2003
  2. Hugo
    Replies:
    10
    Views:
    1,321
    Matt Humphrey
    Oct 18, 2004
  3. Elaine Cheong
    Replies:
    1
    Views:
    1,008
    Gordon Beaton
    Dec 9, 2006
  4. Replies:
    19
    Views:
    1,135
    Daniel Vallstrom
    Mar 15, 2005
  5. sophie_newbie

    Reporting the line number of an exception

    sophie_newbie, May 29, 2008, in forum: Python
    Replies:
    1
    Views:
    325
    Peter Otten
    May 29, 2008
Loading...

Share This Page