Ba7a7chy
Ba7a7chy

Reputation: 1521

Ruby - Extract values from XML using nokogiri

This is my XML:

<Group ID="1" ParentGroupID="5" targetHostname="" Name="127.0.0.1[192.168.0.8]:80 | 81 | 80" LoadBalancerMaxConnections="0" State="1" ProtocolType="TCP" Description="HttpProxy" ProtocolName="HttpProxy">
  <Instance ID="1" Type="CrossProtocol.CrossProtocolHost" NoDelay="False" KeepAlive="False" LocalIP="0.0.0.0" LocalPort="80" ServiceID="-1" MaxConnections="0" RemoteIP="127.0.0.1" RemotePort="80" CallBackPort="81" Description="HttpProxy" DesignatedIP="1.1.1.1" ProtocolName="HttpProxy">
    <IPAddressRestriction Name="Restrict or grant access to services based on IP addresses" Type="0" Description="">
    </IPAddressRestriction>
  </Instance>
  <Instance ID="2" Type="CrossProtocol.CrossProtocolHost" LocalIP="0.0.0.0" LocalPort="81" ServiceID="0" MaxConnections="0" Description="HttpProxy">
    <IPAddressRestriction Name="IPAddress Restriction" Type="0" Description="Restrict or grant access to services based on IP addresses">
    </IPAddressRestriction>
  </Instance>
</Group>

I want to get the value of RemotePort out of the XML to use as a variable, I tried using :

doc.xpath("//Instance/RemotePort")

But I get this as response:

doc.xpath("//Instance/RemotePort")
=> []

What did I do wrong ?

Upvotes: 0

Views: 151

Answers (1)

Uri Agassi
Uri Agassi

Reputation: 37409

doc.xpath('//Instance/@RemotePort')
# => [#<Nokogiri::XML::Attr:0x11615d8 name="RemotePort" value="80">]

doc.xpath('//Instance/@RemotePort').map(&:value)
# => ["80"] 

Upvotes: 3

Related Questions