Ruby: How do I get attribute values from XML with

2019-07-03 23:09发布

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>

Thanks

标签: ruby nokogiri
2条回答
混吃等死
2楼-- · 2019-07-03 23:52
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 result:

#=> ready to use
查看更多
小情绪 Triste *
3楼-- · 2019-07-03 23:52

You subscript them.

doc = Nokogiri::HTML(open('http://google.com'))
doc.css('img:first').first['alt']
=> "Google"
查看更多
登录 后发表回答