开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜