将XML文档导入Rails数据库?

XML*_*yer 3 xml sqlite ruby-on-rails

我一直在阅读教程后的教程,但似乎没有什么事情适合我.目标是获取包含元素和属性的XML文档,并将数据插入数据库中.每个元素/属性都是数据库中的一列,每个条目都是一行.这是我一直在使用的组成XML文档:

<?xml version="1.0"?>
<library>
  <NAME><![CDATA[Favorite Books]]></NAME>
  <book ISBN="11342343">
    <title>To Kill A Mockingbird</title>
    <description><![CDATA[Description#1]]></description>
    <author>Harper Lee</author>
  </book>
  <book ISBN="989894781234">
    <title>Catcher in the Rye</title>
    <description><![CDATA[This is an extremely intense description.]]></description>
    <author>J. D. Salinger</author>
  </book>
  <book ISBN="123456789">
    <title>Murphy's Gambit</title>
    <description><![CDATA[Daughter finds her dad!]]></description>
    <author>Syne Mitchell</author>
  </book>
</library>
Run Code Online (Sandbox Code Playgroud)

所以我希望有一个包含2个条目的表格,每个条目都有ISBN,标题,描述和作者.这是基础知识.(我认为CDATA是完全可选的.如果那是我问题的一部分,那么我们一定要摆脱它...)

最终目标有点复杂.有多本书的多个图书馆.数据库之间有关系,所以我可以从我的Book数据库中引用Library数据库,反之亦然.我完全迷失了,绝对是一个新手,但我有良好的工作计算机知识,并愿意测试和尝试.

我正在使用Rails 3.2.6和默认的SQLite3数据库(3.6.20).我已经安装了REXML,ROXML,LibXML等,并通过API和演练阅读,但事情并没有成功.必须有一种简单的方法可以将XML文档转换为带有Book对象的库对象(带有.name方法)(具有.title,.author,.isbn和.description方法).

任何帮助都是真正的帮助!

更新!

好的,下一个问题.我一直在愚弄这背后的逻辑,并想知道做以下事情的最佳方法......

假设我有这个新的和改进的XML文件.

<?xml version="1.0"?>
<RandomTag>
  <library name='Favorite Books'>
    <book ISBN="11342343">
      <title>TKAM</title>
      <description>Desc1</description>
      <author>H Lee</author>
    </book>
    <book ISBN="989894781234">
      <title>Catcher in the Rye</title>
      <description>Desc2</description>
      <author>JD S</author>
    </book>
  </library>
  <library name='Other Books'>
    <book ISBN="123456789">
      <title>Murphy\'s Gambit</title>
      <description>Desc3</description>
      <author>Syne M</author>
    </book>
  </library>
</RandomTag>
Run Code Online (Sandbox Code Playgroud)

所以现在我们有两个图书馆,第一个名为"喜欢的书籍",有2本书,第二个名为"其他书籍",并有一本书.

每本书知道它属于哪个库的最佳方法是什么?最初,我创建了一个Library数据库和一个Book数据库.每个Book对象都有一个library_id字段,该字段引用了正确的库.因此,每个数据库都可以使用"@ library.books.each do | b | b.title"之类的语法正确填写.然而,这只有在我有一个图书馆时才有用.

我尝试嵌套你在一个类似的库循环中给我的Book循环,但.css方法找到每一个匹配,无论它在哪里.是否有.css方法找到UNTIL特定点?

换句话说,我希望能够将每本书导入各自的图书馆.我无法向XML文件添加任何字段.

再次感谢.

Mik*_*ike 10

我使用Nokogiri库做了类似的事情.

doc = Nokogiri::XML(xml_data)

doc.css('book').each do |node|
  children = node.children

  Book.create(
    :isbn => node['ISBN'],
    :title => children.css('title').inner_text,
    :description => children.css('description').inner_text,
    :author => children.css('author').inner_text
  )
end
Run Code Online (Sandbox Code Playgroud)

更新

您可以通过执行以下操作来创建快速测试:

首先安装nokogiri宝石:

gem install nokogiri
Run Code Online (Sandbox Code Playgroud)

然后创建一个名为text_xml.rb的文件,其中包含以下内容:

require 'nokogiri'

doc = Nokogiri::XML('<?xml version="1.0"?>
  <library>
    <NAME><![CDATA[Favorite Books]]></NAME>
    <book ISBN="11342343">
      <title>To Kill A Mockingbird</title>
      <description><![CDATA[Description#1]]></description>
      <author>Harper Lee</author>
    </book>
    <book ISBN="989894781234">
      <title>Catcher in the Rye</title>
      <description><![CDATA[This is an extremely intense description.]]></description>
      <author>J. D. Salinger</author>
    </book>
    <book ISBN="123456789">
      <title>Murphy\'s Gambit</title>
      <description><![CDATA[Daughter finds her dad!]]></description>
      <author>Syne Mitchell</author>
    </book>
  </library>')

doc.css('book').each do |node|
  children = node.children

  book = {
    "isbn" => node['ISBN'], 
    "title" => children.css('title').inner_text, 
    "description" => children.css('description').inner_text, 
    "author" => children.css('author').inner_text
  }

  puts book
end
Run Code Online (Sandbox Code Playgroud)

最后运行:

ruby test_xml.rb
Run Code Online (Sandbox Code Playgroud)

当你粘贴在你的xml中时,我怀疑你没有逃脱Murphy's Gambit中的单引号.