开发者

XmlSimple - using xml_in() to parse data in Ruby on Rails

开发者 https://www.devze.com 2023-04-09 12:40 出处:网络
I have a simple XML file like this: <Course> <CompanyName value=\"Ford\"/> <DepartmentName value=\"assessments\"/>

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!


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')
0

精彩评论

暂无评论...
验证码 换一张
取 消