Importing an XML document into a Rails database? -
i've been reading through tutorial after tutorial, nothing seems working out me. goal take xml document elements , attributes , insert data in database. each element/attribute column in database, , each entry row. here made-up xml doc i've been working with:
<?xml version="1.0"?> <library> <name><![cdata[favorite books]]></name> <book isbn="11342343"> <title>to kill mockingbird</title> <description><![cdata[description#1]]></description> <author>harper lee</author> </book> <book isbn="989894781234"> <title>catcher in rye</title> <description><![cdata[this extremely intense description.]]></description> <author>j. d. salinger</author> </book> <book isbn="123456789"> <title>murphy's gambit</title> <description><![cdata[daughter finds dad!]]></description> <author>syne mitchell</author> </book> </library>
so want have table 2 entries, each entry having isbn, title, description, , author. that's basics. (the cdata optional suppose. if that's part of problem, means let's rid of it...)
end goal bit more complicated. multiple libraries multiple books. databases have relationship between them, can reference library database book database, , vice versa. i'm lost, rookie, have working computer knowledge , willing test , try things out.
i'm using rails 3.2.6 default sqlite3 database (3.6.20). i've installed rexml, roxml, libxml, etc , read through apis , walkthroughs, things aren't working out. there has easy way turn xml doc library object (with .name method) book objects (having .title, .author, .isbn, , .description methods).
any apprecaited!
update!
okay, next question. i've been fooling around logic behind this, , wanted know best way following...
suppose have new , 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 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>
so have 2 libraries, first titled "favorite books" , having 2 books, second titled "other books" , having single book.
what best way each book know library belongs to? originally, created library database , book database. each book object had library_id field, referenced correct library. thus, each database correctly fill in using syntax "@library.books.each |b| b.title". this, however, worked when had 1 library.
i tried nesting book loop gave me inside similar library loop, .css method finds every single match, regardless of resides. there .css method finds until specific point?
to rephrase, i'd able import each book respective library. can't add fields xml file.
thanks again.
i did similar using nokogiri library.
doc = nokogiri::xml(xml_data) doc.css('book').each |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
update
you create quick test doing this:
first install nokogiri gem:
gem install nokogiri
then create file called text_xml.rb contents:
require 'nokogiri' doc = nokogiri::xml('<?xml version="1.0"?> <library> <name><![cdata[favorite books]]></name> <book isbn="11342343"> <title>to kill mockingbird</title> <description><![cdata[description#1]]></description> <author>harper lee</author> </book> <book isbn="989894781234"> <title>catcher in rye</title> <description><![cdata[this extremely intense description.]]></description> <author>j. d. salinger</author> </book> <book isbn="123456789"> <title>murphy\'s gambit</title> <description><![cdata[daughter finds dad!]]></description> <author>syne mitchell</author> </book> </library>') doc.css('book').each |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
and run:
ruby test_xml.rb
i suspect weren't escaping single quote in murphy's gambit when pasted in xml.
Comments
Post a Comment