Parsing XML in (J) Ruby and pasting into a database

I'm new to (J) Ruby - I wrote some tiny “demos” in RoR but they really didn't get into the syntax.

I have an application currently written in Java that takes an XML file, parses it, and then inserts it into a MySQL database using Hibernate. What I really would like to do is see if I can pass this on to JRuby, mainly as a training exercise, but I'm not quite sure where to start.

This document seems to give a good idea about XML parsing:

http://developer.yahoo.com/ruby/ruby-xml.html

But from there, I'm not sure if the best way to put it in the database is. There would be an ActiveRecord option, and if so, how to connect it to a "stand-alone" JRuby application? Or, I think I could somehow integrate my existing Hibernate stuff with it, right?

Any advice or links to sample code would be greatly appreciated ...

Regards, Andrew.

+3
source share
1 answer

Go ahead and pop up ActiveRecord, here are some of them that started for the h2 built-in java database.

I think you need these gems

jruby -S gem install active_record
jruby -S gem install active_record_jdbc_adapter
jruby -S gem install active_record_jdbch2_adapter
jruby -S gem install jdbc_h2

Then you can use an active record like this

require 'active_record'
require 'logger'

my_logger = Logger.new(STDOUT)
my_logger.level = Logger::DEBUG
ActiveRecord::Base.logger = my_logger

ActiveRecord::Base.establish_connection(
  :adapter => 'jdbch2',
  :database => "my_database_file",        # set to anything you want first run
  :username => "my_username",             # set to anything you want first run
  :password => "my_secret_password"       # set to anything you want first run
)

. ActiveRecord, , . , "migrations" "20090815230000_create_my_models.rb". , "snake_case" CamelCase.

class CreateMyModels < ActiveRecord::Migration
  def self.up
    create_table :my_models do |t|
      t.string  :foo
    end
  end

  def self.down
    drop_table :my_models
  end
end

( script) Rails . rails , , , .

ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("migrations")

, ActiveRecord

class MyModel < ActiveRecord::Base
end

.

x=MyModel.new
x.foo="bar"
x.save!

, . ActiveRecord , , Sequel .

+4

Source: https://habr.com/ru/post/1720248/


All Articles