minidom help -- line number

Thomas Jollans thomas at jollybox.de
Sat Aug 14 13:56:45 EDT 2010


On Saturday 14 August 2010, it occurred to GZ to exclaim:
> On Aug 14, 12:07 pm, Thomas Jollans <tho... at jollybox.de> wrote:
> > On Saturday 14 August 2010, it occurred to GZ to exclaim:
> > > Hi All,
> > > 
> > > I am writing a little program that reads the minidom tree built from
> > > an xml file. I would like to print out the line number of the xml file
> > > on the parts of the tree that are not valid. But I do not seem to find
> > > a way to correspond minidom nodes to line numbers.
> > 
> > The DOM does not contain things like line number information. You work
> > with the structure of the document, not the appearance of the file you
> > happen to be using as a source. You can't use line numbers with minidom.
> > 
> > For stream-based parsers like SAX and eXpat (both in the standard
> > library) this makes more sense, and they both allow you to check the
> > current line number in one way or another.
> 
> So I am basically out of luck if I want to tie back to the original
> file for file error reporting etc. sounds like a deficiency to me.

The DOM is disjunct from the original file, stream, string, etc. That's in the 
nature of the DOM, and it's probably true for most, if not all, DOM 
implementations, in other programming languages as well as Python.
If you want to stay close to the file, you're better of with SAX. (or a 
similar approach, like eXpat)



More information about the Python-list mailing list