InQusitive InQusitive - 1 year ago 123
Ruby Question

How to get nokogiri attribute value?

My xml contains multiple statements like

<House name="bla"><Room id="bla" name="black" ><blah id="blue" name="brown"></blah></Room></House>

I need to get all the values for the given keyword.

I used
nodes = doc.css("[name]")
to get the
<Room id="bla" name="black" ><blah id="blue" name="brown"></blah></Room>

But how do I get the value for a key from this. Is there any easier way to do this?

Answer Source
node_names = doc.css("[name]").map { |node| node['name'] }

for all node names; or for just "black",

black = doc.at_css("[name]")['name']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download