A Real Ruby XML script

Here is a problem that I have been having and I have just solved it via Ruby and REXML:

I have two GPX files that I use for geocaching — one contains all the caches that I want to find and another contains the ones I have already found. Keep track of your finds are important — you can make sure that none of your finds sneaks back into your “soon to find.” What I wanted was a script in which I would give it a list of waypoint names and it would find them in the “everything” pile and move them to the “found” pile. Sounds easy, right? Well, these piles are XML. So now it is no longer trivial.

Yeah, I’ve written DOM scripts before in which I moved nodes from one XML file to another. It ain’t pretty. Since I’ve been liking REXML so much, I decided that I would try tackling it with that and Ruby. My result is called markfound.rb and you can find it here.

The only bad thing about it is the speed. The “everything” pile is 1.3MB (down from 18MB — see a previous post) and takes a while to load. And “found” pile is smaller, so it doesn’t seem to take as long. Of course, both files will get bigger as time goes on. Hopefully, REXML will get faster as that happens. But since the time is spent in loading, I could give it 100 waypoints to move and it wouldn’t be that much slower. Hey, at least I know where the bottleneck is.

There was a surprising thing — a suprise in a good way. What I did was find the tree containing wpt tag that has the proper waypoint name, add it to the found pile and remove it from the everything pile. In DOM, this is about three or four calls. In Ruby/REXML, it was one:

 foundRoot.add(cacheInfo[cache])

REXML knew that if I was adding an element from DocX into DocY that I wanted to remove it from DocX. This feature is genius. Okay, it maybe a bug. But it worked out well for me.

Leave a Reply

You must be logged in to post a comment.