我一直在阅读一本又一篇教程,但似乎没有什么对我有帮助。目标是获取带有元素和属性的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>所以我想要一个包含两个条目的表,每个条目都有一个ISBN、标题、描述和作者。这是最基本的。(我认为CDATA是完全可选的。如果这是我的问题的一部分,那就让我们消除它吧.)
最终目标要复杂一些。有多本书的多个图书馆。数据库有它们之间的关系,所以我可以从我的图书数据库引用Library数据库,反之亦然。我完全迷路了,绝对是个新手,但我有很好的电脑知识,并且愿意测试和尝试。
我在默认的SQLite3数据库(3.6.20)中使用Rails 3.2.6。我已经安装了REXML、ROXML、LibXML等,并阅读了API和演练,但是事情并没有得到解决。必须有一种简单的方法将XML转换为包含.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>所以现在我们有两个图书馆,第一个叫做“最喜欢的书”,有两个书,第二个叫做“其他书”,还有一本书。
对于每本书来说,知道它属于哪个图书馆的最好方法是什么?最初,我创建了一个库数据库和一个图书数据库。每个Book都有一个library_id字段,该字段引用正确的库。因此,每个数据库都可以正确地使用语法来填充,比如"@library.books.each do _b\\ b.title“。然而,这只在我有一个库的时候才起作用。
我尝试在类似的Library循环中嵌套您给我的Book循环,但是.css方法查找每个匹配项,不管它驻留在哪里。是否有找到特定点的.css方法?
为了改头换面,我希望能够将每本书导入各自的图书馆。我不能向XML文件中添加任何字段。
再次感谢。
发布于 2012-07-06 22:51:53
我使用诺科吉里库做了类似的事情。
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 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‘suspect中的单引号。
https://stackoverflow.com/questions/11369911
复制相似问题