jruby下使用nokogiri、xslt
jruby与原生的ruby vm之间最大的区别是多了一个jvm作为中间层,好处是可以借助成熟的java 虚拟机技术,缺点就是不能象以前那样使用很多本地编译的动态链接库,这个问题的解决只能靠为这些库开发java版本来搞定。
?
例如,很多ruby开发者都喜欢nokogiri这个库,它处理html、xml非常方便,但是之前它是基于本地库的,所以jruby环境下不好用,不过现在有了 java 版本
?
$ gem list nokogiri*** LOCAL GEMS ***nokogiri (1.4.4.2 java)
?
不过,实际使用的时候还有一个问题,它需要有xslt哭的支持,否则就会出错:
?
$ rails s/home/john/.rvm/rubies/jruby-1.5.6/lib/ruby/site_ruby/shared/ffi/library.rb:28:in `ffi_lib': Could not?open library 'xslt' : xslt: cannot open shared object file: No such file or directory. Could not open library 'libxslt.so' : libxslt.so: cannot open shared object file: No such file or directory (LoadError)from /home/john/.rvm/rubies/jruby-1.5.6/lib/ruby/site_ruby/shared/ffi/library.rb:10:in `map'from /home/john/.rvm/rubies/jruby-1.5.6/lib/ruby/site_ruby/shared/ffi/library.rb:10:in `ffi_lib'from /home/john/.rvm/gems/jruby-1.5.6/gems/nokogiri-1.4.4.2-java/lib/nokogiri/ffi/libxml.rb:13from /home/john/.rvm/gems/jruby-1.5.6/gems/nokogiri-1.4.4.2-java/lib/nokogiri/ffi/libxml.rb:239:in `require'?
?
但是 ruby-xslt 是不能用在这里的,因为它也不是纯ruby库,因此不能用在jruby上
?
$ gem install ruby-xsltBuilding native extensions. This could take a while...ERROR: Error installing ruby-xslt:ERROR: Failed to build gem native extension. /home/john/.rvm/rubies/jruby-1.5.6/bin/jruby extconf.rbWARNING: JRuby does not support native extensions or the `mkmf' library. Check http://kenai.com/projects/jruby/pages/Home for alternatives.extconf.rb:31: undefined method `enable_config' for main:Object (NoMethodError)?
最后找到的是这个库:
?
$ gem install saxonyFetching: saxony-0.3.3.gem (100%)Successfully installed saxony-0.3.31 gem installedInstalling RDoc documentation for saxony-0.3.3...$ rails s=> Booting WEBrick=> Rails 3.0.5 application starting in development on http://0.0.0.0:3000=> Call with -d to detach=> Ctrl-C to shutdown server[2011-05-21 11:16:11] INFO WEBrick 1.3.1[2011-05-21 11:16:11] INFO ruby 1.8.7 (2010-12-03) [java][2011-05-21 11:16:16] INFO WEBrick::HTTPServer#start: pid=3755 port=3000?
ok,现在可以用 nokogiri 了