将 XML 文档导入 Rails 数据库?

编程入门 行业动态 更新时间:2024-10-25 22:31:03
本文介绍了将 XML 文档导入 Rails 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在阅读一个又一个教程,但似乎对我没有任何帮助.目标是获取包含元素和属性的 XML 文档并将数据插入数据库中.每个元素/属性将是数据库中的一列,每个条目是一行.这是我一直在使用的虚构 XML 文档:

I've been reading through tutorial after tutorial, but nothing seems to be working out for me. The goal is to take a XML document with elements and attributes and insert the data in a database. Each element/attribute will be a column in the database, and each entry is a row. Here is the made-up XML doc that I've been working with:

<?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 是完全可选的.如果这是我的问题的一部分,无论如何让我们摆脱它......)

So I want to have a table with 2 entries, each entry having an ISBN, Title, Description, and Author. That's the basics. (The CDATA is completely optional I suppose. If that's part of my problem, by all means let's get rid of it...)

最终目标有点复杂.拥有多本书的多个图书馆.数据库之间存在关系,因此我可以从我的 Book 数据库中引用 Library 数据库,反之亦然.我完全迷失了,绝对是一个菜鸟,但我有很好的计算机知识,并且愿意测试和尝试.

End goal is a bit more complicated. Multiple libraries with multiple books. Databases have the relationship between them, so I can reference the Library database from my Book database, and vice versa. I'm completely lost, definitely a rookie, but I have a good working computer knowledge and am willing to test and try things out.

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

I'm using Rails 3.2.6 with the default SQLite3 database (3.6.20). I've installed REXML, ROXML, LibXML, etc and read through APIs and walkthroughs, but things just aren't working out. There has to be an easy way to turn the XML doc into a Library object (with a .name method) with Book objects (having .title, .author, .isbn, and .description methods).

真的很感激任何帮助!

更新!

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

Okay, next question. I've been fooling around with the logic behind this, and wanted to know the best way to do the following...

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

Suppose I have this new and improved XML file.

<?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>

所以现在我们有两个图书馆,第一个名为Favorite Books",有 2 本书,第二个名为Other Books",只有一本书.

So now we have two libraries, the first titled "Favorite Books" and having 2 books, the second titled "Other Books" and having a single book.

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

What is the best way for each book to know which library it belongs to? Originally, I created a Library database and a Book database. Each Book object had a library_id field, which referenced the correct Library. Thus, each database could correctly fill in using syntax like "@library.books.each do |b| b.title". This, however, only worked when I had one library.

我尝试将您提供给我的 Book 循环嵌套在一个类似的 Library 循环中,但是 .css 方法会找到每个匹配项,而不管它位于何处.是否有找到 UNTIL 特定点的 .css 方法?

I tried nesting the Book loop you gave me inside a similar Library loop, but the .css method finds every single match, regardless of where it resides. Is there .css method that finds UNTIL a specific point?

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

To rephrase, I'd like to be able to import each book into its respective library. I can't add any fields to the XML file.

再次感谢.

推荐答案

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

I did something similar using the Nokogiri library.

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

更新

您可以通过这样做来创建一个快速测试:

You could create a quick test by doing this:

首先安装 nokogiri gem:

First install the nokogiri gem:

gem install nokogiri

然后创建一个名为 text_xml.rb 的文件,内容如下:

Then create a file called text_xml.rb with the contents:

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 中的单引号.

I suspect you weren't escaping the single quote in Murphy's Gambit when you pasted in your xml.

更多推荐

将 XML 文档导入 Rails 数据库?

本文发布于:2023-10-23 18:54:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   数据库   XML   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!