Ruby, libxml and Windows
Posted by Charlie Fri, 09 Feb 2007 08:06:00 GMT
Let's say my last post tempted you to check out libxml's validation functionality. However, you work in shop that develops on multiple platforms, including Windows, OS X and Linux, so you code has to run on all three platforms.
You happily note that libxml does run on all three platforms, so you download and install it. Next, you fire up your friendly command prompt and ask Ruby Gems to install the appropriate bindings.
Unfortunately, you discover there isn't a pre-built binary for Windows. Being in a hurry, you open up VC++ (experience has taught you that MinGW is a royal pain and will undoubtedly eat up the rest of your day).
Sadly, VC++ doesn't compile the extension. At least with this code, its more pedantic than GCC and flags some sketchy code that increments void pointers. You also note that you have to export the main extension function from the DLL (what developer at Microsoft years ago thought this was a good idea?).
So, to save you the trouble, I've provided a binary that works with Ruby 1.8.5. If you'd like to see the patch, its available at RubyForge.
To use the extesion, copy it into this directory:
ruby\lib\ruby\site_ruby\1.8\i386-msvcrt\xml
Then write code that looks something like this:
require 'xml/libxml_so' def verify(html) dtd = XML::Dtd.new("public", 'xhtml1-transitional.dtd') parse = XML::Parser.string(html) parse.validate(dtd) end
Yup, the "_so" bit is required due to the way the extension is packaged (it shouldn't be, and I was tempted to remove it, but thought it was better to maintain cross-platform compatibility).
Happy hacking!

Thank you, this was exactly what I was looking for and very helpful.
Thanks Brian for the note - glad its helpful.
Charlie,
This looks like exactly what I’m after but it appears that the link to the binary is broken. Can you save me from what would be an slow slog to compile on Windows?
Thanks
Hey Chris - Should work now…let me know if you still have problems.
Hi Charlie, I am getting the below error when I follow the instructions above. I think I might have a different version of dlls than those you used to compile. Which version of mscvrt.dll you are using? If you have any other ideas for getting this to work, let me know! Thanks c:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt/xml/libxml_so.so: 14001: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. - c:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt/xml/libxml_so.so (LoadError)from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’ from script.rb:5
Hi Michael - yeah its hard to tell. I compiled this version with VC++ 2005, which is really annoying about signed assemblies. You’ll need version 8 of the runtime libraries. I recently compiled a new version for the latest bindings (in beta), I’ll put that one online. Probably won’t make a difference though.