rails uses nokogiri to parse xml files

One, cheap nokogiri

gem 'nokogiri'

Execute bundle install
2. Use
NetHelp method

def self.read_xml_file(url_xml)
	xml = Nokogiri::XML(File.open(url_xml))
	xml
end

Call method

xml = NetHelp.read_xml_file("#{Rails.root}/lib/demo.xml")
tds = xml.xpath("//body")
Rails.logger.info "====#{tds[0].content }"
  • Parameter url_xml is the file address
  • tds = xml.xpath("//body") to get all the data containing the body node, the return data format is
==>#  [<body>Don't forget the meeting!</body>,<body>Don't forget the meeting!111</body>,<body>Don't forget the meeting!222</body>]
  • tds[0].content gets the first content in the returned data
==>#  Don't forget the meeting!

Guess you like

Origin blog.csdn.net/weixin_42656358/article/details/102906808