The best kittens, technology, and video games blog in the world.

Tuesday, August 01, 2006

magic/xml

Photo from Commons by Fabian Köster, GFDL.
The promised library for XML-oriented programming with Ruby got implemented :-)

Most of the examples from my last post work. One cool technique I discovered during the coding is "duplicate and modify". It makes very pretty functional-style programming possible:
a = XML.new(:foo, {:a => "1"}, "Hello")
b = a.dup{ @name = :bar }
c = a.dup{ self[:a] = "2" }
d = a.dup{ self << ", world!" }
Basically the optional block given to dup is instance_evaled in context of the new object. I think this is general and cool enough that we can probably find a few uses for it in the standard library.

Everything works Ruby way, so we can do things like:
include XHTML
a = A.new({:href => "http://www.google.com/"}, "Google")
a[:href] = "http://earth.google.com/"
a << " Earth"
The library does not know how to parse XML (wow). It can only generate it, and output formatting is done by simply printing it. I'll take a look at XML-parsers later. It should be trivial to connect one.

DTDs, XML namespaces, explicit entities (< > and the usual stuff gets auto-escaped, so don't worry) get a big meh. If they can be supported without making the common case more complicated, then I'm all for it. If not - you can always use some other library.

Download is here. Unit tests and the blog are the only documentation for now.

6 comments:

Qerub said...

XML Builder is pretty nice.
http://builder.rubyforge.org/

taw said...

Thanks for the link.

It may sound somewhat silly, but I want to check other XML libraries only after I finish magic/xml and write a few small programs in it.

The point is - I don't want to have any preconceptions about how XML processing "should" be done, and just do it the way that feel intuitive. After I have these test programs (let's call them expressiveness benchmarks) I can compare which library is more intuitive (well, at least to me) and modify magic/xml accordingly (or even /dev/null if my way was totally off-track) :-)

Brad said...

What license is magic/xml released under?

taw said...

Brad: Oh yeah, I didn't include a license. Just do whatever you want, or if you need lawyerish stuff here is it:

Copyright (c) 2006-2007 Tomasz Wegrzanowski <Tomasz.Wegrzanowski@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Brad said...
This comment has been removed by the author.
taw said...

Brad: Great, thanks