Is there a way to parse XML via SAX/DOM with line numbers available per node.

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-05-09T16:46:18Z Indexed on 2010/05/09 16:58 UTC
Read the original article Hit count: 251

Filed under:
|
|
|

I already have written a DOM parser for a large XML document format that contains a number of items that can be used to automatically generate Java code. This is limited to small expressions that are then merged into a dynamically generated Java source file.

So far - so good. Everything works.

BUT - I wish to be able to embed the line number of the XML node where the Java code was included from (so that if the configuration contains uncompilable code, each method will have a pointer to the source XML document and the line number for ease of debugging). I don't require the line number at parse-time and I don't need to validate the XML Source Document and throw an error at a particular line number. I need to be able to access the line number for each node and attribute in my DOM or per SAX event.

Any suggestions on how I might be able to achieve this?

P.S. Also, I read the StAX has a method to obtain line number whilst parsing, but ideally I would like to achieve the same result with regular SAX/DOM processing in Java 4/5 rather than become a Java 6+ application or take on extra .jar files.

© Stack Overflow or respective owner

Related posts about java

Related posts about Xml