# XML parser with file names and line numbers

**URL:** <https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374>\
**Category:** Ruby\
**Created:** [April 21, 2006, 12:01am UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374 "2006-04-21T00:01:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 21, 2006, 12:01am UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/1 "2006-04-21T00:01:52Z")

</div>

Howdy,

Is there a Ruby XML parser that includes the file name and line number  
for  
elements?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 21, 2006, 11:42am UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/2 "2006-04-21T11:42:19Z")

</div>

2006/4/21, David P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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…

robert

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 21, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/3 "2006-04-21T14:22:57Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Robert K. wrote:

> 2006/4/21, David P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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](http://enigmail.mozdev.org)

iD8DBQFESM+EMyx0fW1d8G0RAk7tAJ4m+U76yd9Mrb3XQYR+lQ8HqFaHpwCeNmYp  
m1EsIM36/YOm5JHD6Ke1f9E=  
=KWbP  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 21, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/4 "2006-04-21T17:22:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 21, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/5 "2006-04-21T17:31:44Z")

</div>

David P. 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.

No, ruby-libxml is in development. I know because I am an active  
“talker” on the ruby-libxml mailing list.

[http://rubyforge.org/projects/libxml/](http://rubyforge.org/projects/libxml/)

I switched from REXML to libxml because libxml is blazing fast.

Zach

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 21, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/6 "2006-04-21T17:47:35Z")

</div>

Zach,

Yep… libxml does the trick with:  
XML::Parser.default\_line\_numbers = true  
…

element.line\_number  
element.doc.filename

Thanks,

David

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 21, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/7 "2006-04-21T23:36:08Z")

</div>

On Fri, 2006-04-21 at 00:01, David P. wrote:

> Howdy,
> 
> Is there a Ruby XML parser that includes the file name and line number for  
> elements?

XML does not have the concept of a line. XML deals only with describing  
data and structure, not formatting. Carriage returns and line feeds are  
considered to be whitespace.

That is why true XML editors use separate style sheets, like CSS,  
XSL-FO, or FOSI, to format XML documents.

If you have an XML document, process it in some way, for example just by  
parsing it and saving it, any carriage returns and line feeds may have  
been removed. The parser may even add new ones. Whitespace is guaranteed  
to be preserved in CDATA sections only.

You might find it more useful to count the elements themselves. That way  
the numbers won’t change just because you open a file in an editor and  
look at it.

Elements do not have file names either.

What is it you want to do?

/Henrik

–  
[http://kallokain.blogspot.com/](http://kallokain.blogspot.com/) - Blogging from the trenches of software  
development  
[http://www.henrikmartensson.org/](http://www.henrikmartensson.org/) - Reflections on software development  
[http://tocsim.rubyforge.com/](http://tocsim.rubyforge.com/) - Process simulation  
[http://testunitxml.rubyforge.org/](http://testunitxml.rubyforge.org/) - XML test framework  
[http://declan.rubyforge.org/](http://declan.rubyforge.org/) - Declarative XML processing

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 22, 2006, 12:10am UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/8 "2006-04-22T00:10:12Z")

</div>

Henrik,

I’ve already got a solution to the issue. libxml-ruby has the  
functionality  
I need.

One of my projects is SiteMap ( [http://rubyforge.org/projects/sitemap](http://rubyforge.org/projects/sitemap) )  
which is a Domain Specific Language that descripts web site navigation,  
access control, link names, etc. SiteMap allows the designer to imbed  
Ruby  
code (e.g., to test access control, etc.) It would be nice to have the  
file/line of the generated methods in stack traces, etc.

Thanks,

David

---

<div class="post-metadata">

**Author:** ![dpitman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dpitman](https://www.ruby-forum.com/u/dpitman)\
**Post date:** [April 21, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/xml-parser-with-file-names-and-line-numbers/58374/9 "2006-04-21T17:59:55Z")

</div>

On Apr 21, 2006, at 11:21 AM, David P. 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

> > > >
> > 
> > scanning  
> > =KWbP  
> > -----END PGP SIGNATURE-----

* * *

Bob H. – blogs at \<[http://www.recursive.ca/](http://www.recursive.ca/)  
hutch/\>  
Recursive Design Inc. – [http://www.recursive.ca/](http://www.recursive.ca/)  
Raconteur – [http://www.raconteur.info/](http://www.raconteur.info/)  
xampl for Ruby – [http://rubyforge.org/projects/xampl/](http://rubyforge.org/projects/xampl/)
