Using Ruby on XML Files
I need to copy some data from one system to another. On the first system, I can download an xml file. The second system is a ruby on rails website. So I’d like to them upload the xml file to the site and have it enter in the data. But since I’m not that familiar with working with xml files, I thought I’d work on it from the command line first.
In ruby, there’s a gem called nokogiri which works with xml (or html) files. Here’s the sample xml file that I’m using:
<AbstractBook> <Conference>TEST CONFERENCE 2016</Conference> <abstract> <Id>5</Id>Diversity, diversity <Content>This is all about diversity.</Content> <field id="content">This is all about diversity.</field> <PrimaryAuthor> <FirstName>George</FirstName> <FamilyName>Washington</FamilyName> <Email>[email protected]</Email> <Affiliation>WH</Affiliation> </PrimaryAuthor> <Speaker> <FirstName>George</FirstName> <FamilyName>Washington</FamilyName> <Email>[email protected]</Email> <Affiliation>WH</Affiliation> </Speaker> <ContributionType>Abstract</ContributionType> <Track>Diversity and Inclusion </Track> <AcceptedTrack>Diversity and Inclusion </AcceptedTrack> </abstract> <abstract> <Id>6</Id>Higgs, higgs, higgs <Content>I've got *something to say* out the **Higgs.**</Content> <field id="content">I've got *something to say* out the **Higgs.**</field> <PrimaryAuthor> <FirstName>Abe</FirstName> <FamilyName>Lincoln</FamilyName> <Email>[email protected]</Email> <Affiliation>The White House</Affiliation> </PrimaryAuthor> <Speaker> <FirstName>Abe</FirstName> <FamilyName>Lincoln</FamilyName> <Email>[email protected]</Email> <Affiliation>The White House</Affiliation> </Speaker> <ContributionType>None</ContributionType> <Track>Higgs Physics</Track> <AcceptedTrack>Higgs Physics</AcceptedTrack> </abstract> <abstract> <Id>7</Id>Oh the Lovely Higgs <Content>Some interesting info about how pretty the Higgs is.</Content> <field id="content">Some interesting info about how pretty the Higgs is.</field> <PrimaryAuthor> <FirstName>Grover</FirstName> <FamilyName>Cleveland</FamilyName> <Email>[email protected]</Email> <Affiliation>WH</Affiliation> </PrimaryAuthor> <Speaker> <FirstName>Grover</FirstName> <FamilyName>Cleveland</FamilyName> <Email>[email protected]</Email> <Affiliation>WH</Affiliation> </Speaker> <ContributionType>None</ContributionType> <Track>Higgs Physics</Track> </abstract> </AbstractBook>
And here’s the script I wrote:
#!/Users/maryh/Software/rubies/2.2.3/bin/ruby require 'nokogiri' File.open("Abstracts.xml") do |file| @doc2 = Nokogiri::XML(file) elems = @doc2.xpath("//Speaker") myfirst ='Abe' mylast = 'Lincoln' elems.each do |e| if (e.at('FirstName').text == myfirst) && (e.at('FamilyName').text == mylast) puts e.parent.at('Title').text puts e.parent.at('Content').text print "#{e.at('FirstName').text} " print "#{e.at('FamilyName').text}\n" end end end
First, I open the file and open it as an xml file with nokogiri. I then put in elems all the xml fields labelled “Speaker”. I then cycle through elems looking for the string “Abe” and “Lincoln”. If I find them both, I print out the field “Title” and “Content” which are both in the parent field from “Speaker” and then I print out the first and family names.
This is very basic and there are a lot more things that I need to do. I need to match the speaker info with names stored in a model in my rails app. And then update that entry with the title and content if I find a match.