繁体   English   中英

是否将XML文档导入Rails数据库?

[英]Importing an XML document into a Rails database?

我一直在逐个教程地通读,但是似乎没有什么适合我的。 目的是获取带有元素和属性的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>

因此,我想有一个包含2个条目的表,每个条目都有一个ISBN,标题,描述和作者。 这就是基础。 (我想CDATA完全是可选的。如果那是我的问题的一部分,那么一定要摆脱它...)

最终目标要复杂一些。 具有多个书籍的多个图书馆。 数据库之间具有关系,因此我可以从Book数据库中引用Library数据库,反之亦然。 我完全迷路了,绝对是个菜鸟,但是我掌握了很好的计算机知识,并且愿意测试并尝试一下。

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

确实要有任何帮助!

更新!

好的,下一个问题。 我一直在愚弄这个背后的逻辑,并想知道执行以下操作的最佳方法...

假设我有这个新的和改进的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>

因此,现在我们有两个图书馆,第一个图书馆名为“收藏的图书”,有两本书,第二个图书馆名为“其他书籍”,只有一本书。

每本书了解其所属图书馆的最佳方法是什么? 最初,我创建了一个Library数据库和一个Book数据库。 每个Book对象都有一个library_id字段,该字段引用了正确的Library。 因此,每个数据库都可以使用“ @ library.books.each do | b | b.title”之类的语法正确填写。 但是,这仅在我拥有一个库时才有效。

我尝试将您给我的Book循环嵌套在类似的Library循环中,但是.css方法查找每个匹配项,无论它位于何处。 是否有.css方法找到UNTIL的特定点?

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

再次感谢。

我使用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

更新资料

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

首先安装nokogiri gem:

gem install nokogiri

然后创建一个名为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

最后运行:

ruby test_xml.rb

我怀疑您在粘贴xml时没有逃脱Murphy's Gambit中的单引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM