0

I have a simple XML file like this:

    <Course>
     <CompanyName value="Ford"/>
     <DepartmentName value="assessments"/>
     <CourseName value="parts"/>
     <Result>
      <CoreData>
      <Status value="completed"/>

In my controller I have:

    def xml_facil
      require 'xmlsimple'
      config = XmlSimple.xml_in("#{Rails.root}/doc/TestResults/Ford/assessments/mike.xml", { 'KeyAttr' => 'value' })
      @results = config['CourseName']
    end

In my view I have:

    <%= render @results %>

but the error I get is:

    undefined method `formats' for nil:NilClass

I guess my method is returning nil here so how do I fix this so my view will render "parts"? Any help is appreciated!

Michael Leveton
  • 333
  • 2
  • 7
  • 16
  • I think it's returned nil because XmlSimple() did not read the value key in the 'CourseName' tag. I switched to Nokogiri and used a similar method and it rendered in the view. Now I need to figure out how to capture just the "parts" string without the tag. Any suggestions? – Michael Leveton Oct 01 '11 at 04:50

1 Answers1

0

Since you've switched to Nokogiri, you can dig out the value attribute that you're interested in with this:

require 'nokogiri'
doc   = Nokogiri::XML(open("#{Rails.root}/doc/TestResults/Ford/assessments/mike.xml").read)
value = doc.at('CourseName').attr('value')
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • This worked - thanks for all your help. The controller method is now doc.xpath('//Course').map do |i| {'CourseName' => i.xpath('CourseName').attr('value')} end – Michael Leveton Oct 01 '11 at 07:24