From: Bob Hutchison Date: 2006-04-22T00:57:41+09:00 Subject: Re: XML parser with file names and line numbers On Apr 21, 2006, at 11:21 AM, David Pollak wrote: > It is possible with a SAX parser in Java, but the SAX parser in > rexml does > not include file/line information in the "PullEvent" as far as I > can tell. > > I guess rexml is the only XML parser currently under development > for ruby. Well, there is xampl-pp that I wrote. It doesn't change often, that's true, but I *use* it a lot. The most recent version is bundled with xampl (see my signature for where). Xampl-pp is a pull parser. And it keeps track of line and column as best it can (and yes, it can get the column wrong, especially if UTF is involved (it is sometimes more of a byte count than a character count), but the line count is normally pretty good). The instance variable @input in the parser is a bit of a funny thing, but if it is a file, then you can use the File methods (e.g. path) and that'll work -- the trouble is that @input isn't exposed, so... There are two ways in which the pull parser can be used: as an object that parses and manipulated by calling methods on it, or, alternatively, by extending the parser with actions. I use both techniques, sometimes at the same time -- this can be interesting. Anyway, you can either re-open Xampl_PP and define an accessor for @input, or you can extend and it is right there for you. I've never exposed a reader to @input because mucking with it would be a very very bad idea. Have you looked at the libxml wrapper? It probably provides that information. Cheers, Bob > > On 4/21/06, zdennis wrote: >> >> -----BEGIN PGP SIGNED MESSAGE----- >> Hash: SHA1 >> >> Robert Klemme wrote: >>> 2006/4/21, David Pollak : >>> >>>> Howdy, >>>> >>>> Is there a Ruby XML parser that includes the file name and line >>>> number >> for >>>> elements? >>> >>> >>> What exactly do you mean by that? AFAIK there is no place to store >>> this info in DOM so... >> >> It seems like this would be possible with a SAXParser when you're >> scanning >> the document to be able to grab what lineno an element >> is on when it starts an element. >> >> Zach >> -----BEGIN PGP SIGNATURE----- >> Version: GnuPG v1.4.1 (GNU/Linux) >> Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org >> >> iD8DBQFESM+EMyx0fW1d8G0RAk7tAJ4m+U76yd9Mrb3XQYR+lQ8HqFaHpwCeNmYp >> m1EsIM36/YOm5JHD6Ke1f9E= >> =KWbP >> -----END PGP SIGNATURE----- >> >> ---- Bob Hutchison -- blogs at Recursive Design Inc. -- Raconteur -- xampl for Ruby --