Hello,
I’m absolutely new to ruby and I’m trying to parse a XML structure and
filter it for some attributes. The XML looks like this:
I want to check if the status attribute is OK. So far I have written
this code:
#!/usr/bin/ruby -w
require ‘rubygems’
require ‘net/http’
require ‘xmlsimple’
url = URI.parse(“URL to XML”)
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
sysinfodoc = XmlSimple.xml_in(res.body)
sysinfodoc[“machines”][0][“machine”][0].each do |status|
p status
end
which gives me the following output:
[“name”, “localhost”]
[“vizqlserver”, [{“worker”=>“localhost:9100”, “status”=>“OK”},
{“worker”=>“localhost:9101”, “status”=>“OK”}]]
[“dataengine”, [{“worker”=>“localhost:27042”, “status”=>“OK”}]]
[“repository”, [{“worker”=>“localhost:8060”, “status”=>“OK”}]]
[“webserver”, [{“worker”=>“localhost:80”, “status”=>“OK”}]]
[“backgrounder”, [{“worker”=>“localhost:8250”, “status”=>“OK”}]]
[“serverwebapplication”, [{“worker”=>“localhost:8000”, “status”=>“OK”},
{“worker”=>“localhost:8001”, “status”=>“OK”}]]
[“dataserver”, [{“worker”=>“localhost:9700”, “status”=>“OK”},
{“worker”=>“localhost:9701”, “status”=>“OK”}]]
How can I filter this so I get just the status for every machine child?
Or is this the wrong approach alltogether?
EDIT:
I have changed my script a bit and now it kinda starts outputting what
I’m looking for.
#!/usr/bin/ruby -w
require ‘rubygems’
require ‘net/http’
require ‘xmlsimple’
url = URI.parse(“URL to XML”)
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
sysinfodoc = XmlSimple.xml_in(res.body)
sysinfodoc[“machines”][0][“machine”][0].each do |status|
p status[1][0]
p status[1][1]
end
Output:
{“worker”=>“localhost:8060”, “status”=>“OK”}
nil
{“worker”=>“localhost:27042”, “status”=>“OK”}
nil
{“worker”=>“localhost:9100”, “status”=>“OK”}
{“worker”=>“localhost:9101”, “status”=>“OK”}
{“worker”=>“localhost:8000”, “status”=>“OK”}
{“worker”=>“localhost:8001”, “status”=>“OK”}
{“worker”=>“localhost:8250”, “status”=>“OK”}
nil
{“worker”=>“localhost:9700”, “status”=>“OK”}
{“worker”=>“localhost:9701”, “status”=>“OK”}
{“worker”=>“localhost:80”, “status”=>“OK”}
nil
108
111
How do I get rid of the nils and the Fixnums?
Regards,
Fabian