views:

59

answers:

2

Is there a way to lookup the line number that a given element is at in an xml file via the w3c dom api?

My use case for this is that we have 30,000+ maps in kml/xml format. I wrote a unit test that iterates over each file found on the hard drive (about 17GB worth) and tests that it is parseable by our application. When it fails I throw an exception that contains the element instance that was considered "invalid". In order for our mapping department (nobody here knows how to program) to easily track down the typo we would like to log the line number of the element that caused the exception.

Can anybody suggest a way to do this? Please note we are using the W3C dom api included in the Android 1.6 SDK.

+1  A: 

I'm not sure whether the Android API is different, but a normal Java application could catch a SAXParseException when parsing and look at the line number.

erickson
I am not using SAX, I am using the w3c dom api.
Benju
@Benju - a `DocumentBuilder` throws a `SAXException`, even though it's building a `org.w3c.dom.Document`. If you are using an API outside the Java Standard Edition, please post a link for comparison.
erickson
Dom api is using SAX to parse the xml, so @erickson is right, it will give you a line number of an erroneous element. You can not get a line number of an element from already parsed DOM tree.
tulskiy
The XML is valid, it is the content of the XML that is invalid given the specification of our file format (which just adds info to standard kml/google-earth files).
Benju
+1  A: 

I may be wrong, but the line number shouldn't be relevant to your XML parser/reader as long as the XML structure itself is valid.

You might try to extrapolate the line-number programatically on the assumption that each node/content must be on a distinct line but it's going to be tricky.

Everyone