Search code examples
rubynokogiriwatirsavon

Read value from a Savon response?


I want to read the value of customer.customer_info_id from the following response. My response also includes namespaces:

<Field name="customer.customer_id" value="5403699387967341892"/>
<Field name="**customer.customer_info_id**" value="5403699387967341892"/>
<Field name="customer.customer_since_code" value="1985">
    <Lookup language="EN" value="1985"/>
    <Lookup language="FR" value="1985"/>
</Field>

I tried the following:

# Savon code tried:        

doc = Nokogiri::XML(response.to_xml)
doc.remove_namespaces!
val = doc.xpath("//Field:name" => "Customer.entity_id").to_s
puts "val is: #{val}"

It returns null value.


Solution

  • The below code using Nokogiri worked out to read a particular xml element value:

    doc=Nokogiri.XML(File.open(File.dirname("your file name here"))
    
    element=doc.at('element_name') #fetch the value of element needed.