Ruby: How to get attribute values ​​from XML using Nokogiri?

How to get the value of the message value ("ready to use")?

<?xml version="1.0" encoding="UTF-8"?>
<response status="ok" permission_level="admin" message="ready to use" cached="0">
<title>kit</title>
</response>

thank

+3
source share
2 answers
require 'rubygems'
require 'nokogiri'

string = %Q{
  <?xml version="1.0" encoding="UTF-8"?>
  <response status="ok" permission_level="admin" message="ready to use" cached="0">
  <title>kit</title>
  </response>
}

doc = Nokogiri::XML(string)
doc.css("response").each do |response_node|
  puts response_node["message"]
end

save and run this ruby ​​file, you will get the result:

#=> ready to use
+4
source

You index them.

doc = Nokogiri::HTML(open('http://google.com'))
doc.css('img:first').first['alt']
=> "Google"
0
source

All Articles