aspose file tools*
The moose likes XML and Related Technologies and the fly likes Using DOM - Can I get the line number for a node Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Using DOM - Can I get the line number for a node" Watch "Using DOM - Can I get the line number for a node" New topic
Author

Using DOM - Can I get the line number for a node

PETER LAKE
Greenhorn

Joined: Aug 22, 2001
Posts: 4
Howdy folks,
I am using DOM to parse an XML document. I am validating the document (not in XML/DTD sense) to ensure it is ready to be used by the server software.
At a particular node (which may have an error) I would like to return the offending line number to the user. Is this possible? I know if I was using SAX parser - I could get the Locator. Can't seem to do this when using DOM.
Peter.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
It's not a 100% sure thing, but AFIK you should implement the ErrorHandler interface. It should return you the line number on which the error occured.
You set it like this
aDocumentBuilder.setErrorHandler(new anErrorHandler);
It has 3 methods to be implemented and should do the trick.
PETER LAKE
Greenhorn

Joined: Aug 22, 2001
Posts: 4
Sven,
Thanks for the reply. I would agree with you if it was a parser error that occurred - for example a missing tag. But I determine the error via routines that I have written. I want to report the line number of the node that I have a problem with.
Peter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using DOM - Can I get the line number for a node
 
Similar Threads
xml and javascript
XML Parser
JAXP-DOM Node Import Issue
Invalid/Special XML characters such as &, ', "
read the values from xml using java